java基础知识0719

来源:互联网 发布:mysql的存储过程 编辑:程序博客网 时间:2024/06/05 15:31
###07.01_面向对象(构造方法Constructor概述和格式)(掌握)
A:构造方法概述和作用
    * 给对象的数据(属性)进行初始化
B:构造方法格式特点
    * a:方法名与类名相同(大小也要与类名一致)
    * b:没有返回值类型,连void都没有
    * c:没有具体的返回值return;

###07.02_面向对象(构造方法的重载及注意事项)(掌握)
A:案例演示
    * 构造方法的重载
    * 重载:方法名相同,与返回值类型无关(构造方法没有返回值),只看参数列表
B:构造方法注意事项
    * a:如果我们没有给出构造方法,系统将自动提供一个无参构造方法。
    * b:如果我们给出了构造方法,系统将不再提供默认的无参构造方法。
        * 注意:这个时候,如果我们还想使用无参构造方法,就必须自己给出。建议永远自己给出无参构造方法
        
###07.03_面向对象(给成员变量赋值的两种方式的区别)
A:setXxx()方法
    * 修改属性值 
B:构造方法
    * 给对象中属性进行初始化 

###07.04_面向对象(学生类的代码及测试)(掌握)
A:案例演示
    * 学生类:
        * 成员变量:
            * name,age
        * 构造方法:
            * 无参,带两个参
        * 成员方法:
            * getXxx()/setXxx()
            * show():输出该类的所有成员变量值
B:给成员变量赋值:
    * a:setXxx()方法
    * b:构造方法
    
C:输出成员变量值的方式:
    * a:通过getXxx()分别获取然后拼接
    * b:通过调用show()方法搞定
    *class Gouzao 
{
    public static void main(String[] args) 
    {
        Student s =new Student();
        Student s1 =new Student(20);
          //s1=new Student(88);
          //s1.setAge(11);
        Student s2 =new Student("建文",18);
        System.out.println(s.getAge());
        System.out.println(s1.getAge());
        System.out.println(s2.getAge()+"   "+s2.getName());

       s.show();
    }
}

class Student
{
    private  int age;
    private  String  name;

  public Student()
  {
     
  }
  public Student(int age)
  {
     this.age=age;
  }
  public Student(String name)
  {
      this.name= name;
  }
   public Student(String name,int age)
  {
      this.name=name;
       this.age=age;
  }

public  void   setAge(int age)
{
  this.age=age;
}
public  int  getAge()
{
  return this.age;
}
public  void   setName(String name)
{
  this.name=name;
}
public  String getName()
{
  return this.name;
}
public void show()
    {
        System.out.print(this.getAge()+"  ");
        System.out.println(this.getName());
    }

}


       
        

###07.05_面向对象(手机类的代码及测试)(掌握)
A:案例演示
    * 模仿学生类,完成手机类代码
    *class Shouji 
{
    public static void main(String[] args) 
    {   
           Phone s =new Phone();
        Phone s1 =new Phone(2000);
          s1=new Phone(888);
          s1.setPrice(1888);
        Phone s2 =new Phone("小米",1999);
        System.out.println(s.getPrice());
        System.out.println(s1.getPrice());
        System.out.println(s2.getPrice()+"   "+s2.getBrand());

       s1.show();

    }
}

class Phone
{
    private  int price;
    private  String  brand;

  public Phone()

  {
     
  }
  public Phone(int price)
  {
     this.price=price;
  }
  public Phone(String brand)
  {
      this.brand=brand;
  }
   public Phone(String brand,int peice)
  {
      this.price=price;
       this.brand=brand;
  }

public  void   setPrice(int price)
{
  this.price=price;
}
public  int  getPrice()
{
  return this.price;
}
public  void   setBrand(String Brand)
{
  this.brand=brand;
}
public  String getBrand()
{
  return this.brand;
}
public void show()
    {
        System.out.print(this.getPrice()+"  ");
        System.out.println(this.getBrand());
    }

}


###07.06_面向对象(创建一个对象的步骤)(掌握)
A:画图演示
    * 画图说明一个对象的创建过程做了哪些事情?
    * Student s = new Student();
    * 1,Student.class加载进内存
    * 2,声明一个Student类型引用s
    * 3,在堆内存创建对象,
    * 4,给对象中属性默认初始化值
    * 5,属性进行显示初始化
    * 6,构造方法进栈,对对象中的属性赋值,构造方法弹栈
    * 7,将对象的地址值赋值给s

###07.07_面向对象(长方形案例练习)(掌握)
A:案例演示
    * 需求:
        * 定义一个长方形类,定义 求周长和面积的方法,
        * 然后定义一个测试类进行测试。
        *class RectAngle_Text 
{
    public static void main(String[] args) 
    {
        RectAngle r1= new RectAngle();
         int mj= r1.s(100,200);
         int zc = r1.c(200,300);
         System.out.println("面积"+mj);
        System.out.println("周长"+zc);
        RectAngle r2= new RectAngle(100,30);//调用不同的需要定义不同的方法
          int  mj2=r2.s1();
          System.out.println("面积"+mj2);
    }
}
/*class  RectAngle
{
    private int width;
    private int heigth;

public RectAngle()
{
}

public RectAngle(int x,int y)
{
   this.width=x;
   this.heigth=y;
}
public  void  setWidth(int width)
{
   this.width=width;
}
public int getWidth()
{
   return this.width;
}
public  void  setHeigth(int heigth)
{
   this.heigth=heigth;
}
public int getHeigth()
{
   return this.heigth;
}
public int  c(int width,int heigth)
{
   return (width+heigth)*2;
}
public int s(int width,int heigth)
{
   return width*heigth;
}

public int  c1()
{
   return (width+heigth)*2;
}
public int s1()
{
   return width*heigth;
}


}*/

###07.08_面向对象(员工类案例练习)(掌握)
A:案例演示
    * 需求:定义一个员工类Employee
    * 自己分析出几个成员,然后给出成员变量
        * 姓名name,工号id,工资salary 
    * 构造方法,
        * 空参和有参的
    * getXxx()setXxx()方法,
    * 以及一个显示所有成员信息的方法。并测试。
        * work 
        *class Employee_test 
{
    public static void main(String[] args) 
    {
        Employee yj=new Employee(20140521,"杨佳",200000);
        yj.work();
    }
}
class Employee 
{
    private int id;
    private int salary;
    private String name;

    public Employee()
    {  
    }
    public Employee(int id, String name)
    {
      this.id=id;
      this.name=name;
    }
    public Employee(int id, String name,int salary)
    {
      this.id=id;
      this.name=name;
      this.salary=salary;
    }
    public  void  setId(int id)
    {
        this.id=id;
    }
    public int getId()
    {
        return this.id;
    }
    public  void  setSalary(int salary)
    {
      this.salary=salary;
    }
    public int getSalary()
    {
      return this.salary;
    }
     public  void  setName(String  name)
    {
      this.name=name;
    }
    public String getName()
    {
      return this.name;
    }
    public void work()
    {
        System.out.println(id+name+salary);
    }

}


###07.09_面向对象(static关键字及内存图)(了解)
A:案例演示
    * 通过一个案例引入static关键字。
    * 人类:Person。每个人都有国籍,中国。
    *class Guoji 
{   public static  String gj="日本人";
    public static void main(String[] args) 
    {   
        Guoji d1=new Guoji();
        Guoji d2=new Guoji();
        Guoji d3=new Guoji();
        Guoji d4=new Guoji();
         d1.gj="中国";//根据内部原理改变一个其他的也一起改变了
        System.out.println(d2.gj);
        System.out.println(Guoji.gj);
    }
}

B:画图演示
    * 带有static的内存图
 
###07.10_面向对象(static关键字的特点)(掌握)
A:static关键字的特点
    * a:随着类的加载而加载
    * b:优先于对象存在
    * c:被类的所有对象共享
        * 举例:咱们班级的学生应该共用同一个班级编号。
        * 其实这个特点也是在告诉我们什么时候使用静态?
            * 如果某个成员变量是被所有对象共享的,那么它就应该定义为静态的。
        * 举例:
            * 饮水机(用静态修饰)
            * 水杯(不能用静态修饰)
            * 共性用静态,特性用非静态
    * d:可以通过类名调用
        * 其实它本身也可以通过对象名调用。
        * 推荐使用类名调用。
        * 静态修饰的内容一般我们称其为:与类相关的,类成员
B:案例演示
    * static关键字的特点


###07.11_面向对象(static的注意事项)(掌握)
A:static的注意事项
    * a:在静态方法中是没有this关键字的
        * 如何理解呢?
            * 静态是随着类的加载而加载,this是随着对象的创建而存在。
            * 静态比对象先存在。
    * b:静态方法只能访问静态的成员变量和静态的成员方法
        * 静态方法:
            * 成员变量:只能访问静态变量
            * 成员方法:只能访问静态成员方法
        * 非静态方法:
            * 成员变量:可以是静态的,也可以是非静态的
            * 成员方法:可是是静态的成员方法,也可以是非静态的成员方法。
        * 简单记:
            * 静态只能访问静态。
B:案例演示
    * static的注意事项

###07.12_面向对象(静态变量和成员变量的区别)(掌握)
静态变量也叫类变量  成员变量也叫对象变量
A:所属不同
    * 静态变量属于类,所以也称为为类变量
    * 成员变量属于对象,所以也称为实例变量(对象变量)
B:内存中位置不同
    * 静态变量存储于方法区的静态区
    * 成员变量存储于堆内存
C:内存出现时间不同
    * 静态变量随着类的加载而加载,随着类的消失而消失
    * 成员变量随着对象的创建而存在,随着对象的消失而消失
D:调用不同
    * 静态变量可以通过类名调用,也可以通过对象调用
    * 成员变量只能通过对 象名调用

###07.13_面向对象(main方法的格式详细解释)(了解)
A:格式
    * public static void main(String[] args) {}
B:针对格式的解释
    * public 被jvm调用,访问权限足够大。
    * static 被jvm调用,不用创建对象,直接类名访问
    * void被jvm调用,不需要给jvm返回值
    * main 一个通用的名称,虽然不是关键字,但是被jvm识别
    * String[] args 以前用于接收键盘录入的
C:演示案例
    * 通过args接收键盘例如数据

###07.14_面向对象(工具类中使用静态)(了解)
A:制作一个工具类
    * ArrayTool
    * 1,获取最大值
    * 2,数组的遍历
    * 3,数组的反转
    *class ArrayTool_text 
{
    public static void main(String[] args) 
    {
    int[] arr={1,2,3,4,5,76,78,8,81,9,90,0,6,55,5,45};
     int max= ArrayTool.max(arr);
     System.out.println(max);

      //ArrayTool e=new ArrayTool();//不建议使用这种方法,繁琐
      // int w= e.max(arr);
      // System.out.println(w);

     System.out.println("-------------------------");
     ArrayTool.bianli(arr);
      System.out.println("-------------------------");
      arr =ArrayTool.fangzhuang(arr);
      
      for (int i=0;i<arr.length;i++ )
      {
        System.out.println(arr[i]);
      }
    }
}

class  ArrayTool
{
    public static  int max(int[] arr)
    {
       int max=arr[0];
       for (int i=0;i<arr.length; i++)
        {  
         if (arr[i]>max)
         { max=arr[i];}  
        } 
      return max;
    }
    
   public static void bianli(int[] arr)
    {
       for (int i=0;i<arr.length ;i++ )
       {
          System.out.println(arr[i]);
       }
    }
   
   public  static int[] fangzhuang(int[] arr)
    {
       for (int i=0 ;i<arr.length/2 ; i++)
       {
           arr[i]=arr[arr.length-i-1]^arr[i];
           arr[arr.length-i-1]=arr[i]^arr[arr.length-i-1];
           arr[i]=arr[i]^ arr[arr.length-i-1];
       }
       return arr;
    }
}
###07.15_面向对象(说明书的制作过程)(了解)
A:对工具类加入文档注释
B:通过javadoc命令生成说明书
    * @author(提取作者内容)
    * @version(提取版本内容)
    * javadoc -d 指定的文件目录 -author -version ArrayTool.java
    * @param 参数名称//形式参数的变量名称@return 函数运行完返回的数据

###07.16_面向对象(如何使用JDK提供的帮助文档)(了解)
A:找到文档,打开文档
B:点击显示,找到索引,出现输入框
C:你应该知道你找谁?举例:Scanner
D:看这个类的结构(需不需要导包)
    * 成员变量    字段
    * 构造方法    构造方法
    * 成员方法    方法

###07.17_面向对象(学习Math类的随机数功能)(了解)
打开JDK提供的帮助文档学习
A:Math类概述
    * 类包含用于执行基本数学运算的方法
B:Math类特点
    * 由于Math类在java.lang包下,所以不需要导包。
    * 因为它的成员全部是静态的,所以私有了构造方法
C:获取随机数的方法
    * public static double random():返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
D:我要获取一个1-100之间的随机数,肿么办?
    * int number = (int)(Math.random()*100)+1;
    
###07.18_面向对象(猜数字小游戏案例)(了解)
A:案例演示
    * 需求:猜数字小游戏(数据在1-100之间)
    *import java.util.Scanner;
class Shuiji 
{     
     
    
    public static void main(String[] args) 
    {   
        Scanner sc = new Scanner(System.in);
        int number = (int)(Math.random()*100)+1;
        boolean flag=true;
        while (flag)
        {
           int x = sc.nextInt();
          if (x>number)
          {
             System.out.println("大了");
          }else if (x<number)
          {
             System.out.println("小了");
          }else
          {
             System.out.println("猜对了");
             flag=false;
          }
        }
    
    }
     
}
原创粉丝点击