Java-day11 study notes

day10 review

Insert picture description here

One, the memory map of the object

① An object memory map

package com.ujiuye.javaclass;

/*
    人类
 */
class Person {
    
    

    //成员变量
    //姓名
    String name;
    //年龄
    int age;

    //成员方法
    //吃饭
    public void eat(){
    
    
        System.out.println("吃饭");
    }
    //睡觉
    public void sleep(){
    
    
        System.out.println("睡觉");
    }

}
/*
    测试类
 */
public class Demo {
    
    

    public static void main(String[] args) {
    
    

        //创建对象
        Person p = new Person();

        //获取成员变量
        System.out.println(p.name + "---" + p.age);

        //给成员变量赋值
        p.name = "关晓彤";
        p.age = 25;

        //获取成员变量
        System.out.println(p.name + "---" + p.age);

        //调用方法
        p.eat();
        p.sleep();
        
    }
}

Insert picture description here

② Memory diagram of two objects

class Cat {
    
    

    String name;
    int age;

    public void eat(){
    
    
        System.out.println("吃饭");
    }
    public void sleep(){
    
    
        System.out.println("睡觉");
    }
}

public class Demo2 {
    
    

    public static void main(String[] args) {
    
    

        Cat c = new Cat();
        Cat c2 = new Cat();

        c.name = "小黑";
        c.age = 2;

        c2.name = "小白";
        c2.age = 1;

        System.out.println(c.name + "---" + c.age);
        System.out.println(c2.name + "---" + c2.age);

        c.eat();
        c.sleep();
        System.out.println("----------------------");
        c2.eat();
        c2.sleep();
    }
}

Insert picture description here

Two, the difference between member variables and local variables

成员变量:定义在类中方法外的变量
局部变量:定义方法声明上或方法内部的变量

声明的位置不同:
	局部变量:方法声明上或方法内部
	成员变量:类中方法外
内存中的位置不同:
	局部变量:栈上
	成员变量:堆上
生命周期不同:
	局部变量:随着方法的调用而存在,随着方法的调用结束而消失
	成员变量:随着对象的创建而存在,随着对象的销毁而消失
初始化值的不同:
	局部变量:必须要进行初始化才能使用
	成员变量:不用进行初始化就可以使用,因为成员变量有默认的初始化值
		默认的初始化值:
			整数类型: 		0
			小数类型: 		0.0
			布尔类型: 		false
			字符类型: 		"\u0000"
			引用数据类型:    null

Three, packaging

Encapsulation is to hide the properties and specific implementation details of things, and only provide public access to the outside

/*
    在成员变量的赋值过程中:给了一个非法的数据,这个很明显是不能让其存在的,所以要立刻解决这个问题。
    如何解决?
        1. 通过判断看是否是合理的年龄
        2. 而判断一般是通过分支结构来判断
        3. 分支结构一般放在方法中的,所以我们应该定义一个方法
    方法定义在哪里呢?
        1. 方法都是放在类中的
    选择哪个类?
        1. 因为Demo类主要是用来测试的,里面一般不放功能性代码,所以不选择
        2. 我们应该将这个方法放在Person类中
    方法设定之后,并没有被调用,而还是直接访问的成员变量,怎么办?
        1. 只要让外界不能直接访问成员变量

    Java中提供了一个关键字private,用private修饰的变量,只能在本类中被访问


 */
class Person {
    
    

    String name;
    private int age;

    //设置年龄的方法
    public void setAge(int a){
    
    

        //将外界传递过来的a赋值给成员变量age
        if(a >= 0 && a <= 100){
    
    
            age = a;
        }else{
    
    
            System.out.println("年龄非法");
        }

    }

    //获取成员变量值
    public void show(){
    
    
        System.out.println(name + "===" + age);
    }

}

public class Demo {
    
    

    public static void main(String[] args) {
    
    

        Person p = new Person();

        p.name = "蔡徐坤";
//        p.age = -200;
        p.setAge(60);

//        System.out.println(p.name + "===" + p.age);
        p.show();
    }
}

3.1 Features

1. 隐藏了事物的属性
2. 隐藏了事物的具体实现细节
3. 对外提供公共的访问方式

3.2 private keyword

The private keyword can modify member variables and member methods, and the modified members can only be accessed in this class

3.3 getXxx and setXxx methods

Because member variables in a class are generally required to be privately modified (members modified by private cannot be accessed by the outside world), we need to provide a public method in the class that can be used to set and get member variables Value. The name of the method is generally named getXxx and setXxx methods by us

3.4 this keyword

The this keyword is an object of a class.

Which object calls the method, the this in the method refers to which object [Master]

/*
    人类
 */
public class Person {
    
    

    private String name;
    private int age;

    //getXxx和setXxx
    public void setName(String name){
    
    
        /*
            变量的访问原则:
            1. 先在局部范围内找,如果找到就使用
            2. 如果局部范围内找不到,那么就去成员范围去找到,如果找到就使用
            3. 如果成员范围内找不到,那么就报错
         */
        this.name = name;
    }
    public String getName(){
    
    
        return name;
    }
    public void setAge(int age){
    
    
        this.age = age;
    }
    public int getAge(){
    
    
        return age;
    }

    //展示成员变量
    public void show(){
    
    
        System.out.println(name + "---" + age);
    }
}

/*
    测试类
 */
public class Demo {
    
    

    public static void main(String[] args) {
    
    

        Person p = new Person();
        Person p2 = new Person();

        p.setName("张三");
        p.setAge(33);
        System.out.println(p.getName() + "---" + p.getAge());

        System.out.println("-----------");
        p2.setName("王五");
        p2.setAge(55);
        System.out.println(p2.getName() + "---" + p2.getAge());

    }
}

Four, construction method

The construction method is to create the object and initialize the members

The construction method is automatically called by the jvm virtual machine for us when the object is created.

4.1 Format

1.构造方法的方法名和类名保持一致
2. 构造方法没有返回值,连void都没有
3. 构造方法一般被public修饰
4. 构造方法可以被重载
5. 构造方法的return语句为"return;", 一般都是省略的

public 类名(参数列表){
	//return;
}

4.2 Matters needing attention

1. 所有类都默认带有无参的构造方法
2. 一旦声明一个无参或者带参的构造方法,那么会将默认的无参构造方法给覆盖掉
3. 构造方法可以重载,一般一个类中都有一个无参和一个带参构造方法

Five, the member methods in the class

  • Return value

    • Has return value

      public String getName(){
              
              
      	return name;
      }
      
    • No return value

      public void setName(String naem){
              
              
      	this.name = name;
      }
      
      public void show(){
              
              
          
      }
      
  • With or without parameters

    • With participation

      public void setAge(int age){
              
              
      	this.age = age;
      }
      
    • No parameters

      public int getAge(){
              
              
      	return age;
      }
      

Six, the standard way of writing a class

成员变量
	一般来说成员变量都是被private进行修饰
成员方法
	一般都有setXxx和getXxx方法,包括其它的成员方法
构造方法
	一般都带有无参和带参构造方法
	注意:一般都是用无参构造方法创建对象,而用带参构造方法初始化数据
/*
    人类
 */
public class Person {
    
    

    //成员变量
    //姓名
    private String name;
    //年龄
    private int age;

    //构造方法
    //无参构造方法
    public Person(){
    
    

    }
    //带参构造方法
    public Person(String name){
    
    
        this.name = name;
    }
    public Person(String name, int age){
    
    
        this.name = name;
        this.age = age;
    }

    //成员方法
    //setXxx
    public void setName(String name){
    
    
        this.name = name;
    }
    public void setAge(int age){
    
    
        this.age = age;
    }
    //getXxx
    public String getName(){
    
    
        return name;
    }
    public int getAge(){
    
    
        return age;
    }
    //展示内容的show方法
    public void show(){
    
    
        System.out.println(name + "---" + age);
    }

}

public class Demo {
    
    

    public static void main(String[] args) {
    
    

        //第一种方式:
        //1. 创建对象
        Person p = new Person();

        //2. 设置数据
        p.setName("杨紫");
        p.setAge(25);

        //3. 获取数据
        System.out.println(p.getName() + "---" + p.getAge());
        System.out.println("----------------------------------");

        //第二种方式:
        //1. 创建对象
        Person p2 = new Person("乔碧萝", 60);

        //2. 获取数据
        p2.show();

    }
}
练习:
	写一个标准的手机类,并使用两种方式进行测试
/*
    手机类
 */
public class Phone {
    
    
    //成员变量
    //品牌
    private String brand;
    //价格
    private int price;
    //颜色
    private String color;

    //构造方法
    public Phone() {
    
    

    }

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

    //成员方法
    //setXxx和getXxx
    //brand
    public void setBrand(String brand) {
    
    
        this.brand = brand;
    }

    public String getBrand() {
    
    
        return brand;
    }

    //price
    public void setPrice(int price) {
    
    
        this.price = price;
    }

    public int getPrice() {
    
    
        return price;
    }

    //color
    public void setColor(String color) {
    
    
        this.color = color;
    }

    public String getColor() {
    
    
        return color;
    }

    //show
    public void show() {
    
    
        System.out.println(color + "的" + brand + "手机价格是:" + price);
    }
}
/*
    测试类
 */
public class Demo {
    
    
    public static void main(String[] args) {
    
    
        //方式一
        //1. 创建对象
        Phone p = new Phone();
        //2. 赋值
        p.setBrand("小米");
        p.setPrice(5555);
        p.setColor("black");
        //3. 展示
        p.show();
        System.out.println(p.getBrand() + "---" + p.getPrice() + "---" + p.getColor());

        //方式二
        //1. 创建对象并赋值
        Phone p2 = new Phone("华为", 8888, "pink");
        //2. 展示
        p2.show();
        System.out.println(p2.getBrand() + "---" + p2.getPrice() + "---" + p2.getColor());
    }
}

Guess you like

Origin blog.csdn.net/ChangeNone/article/details/112536909