Using java classes and objects

Continue to create, accelerate growth! This is the 4th day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event

Use of classes and objects

3.1 Class Definition

Object-oriented is to describe and represent thousands of things and objects through classes and objects. First of all, we need to know how to define a class.

The composition of a class is composed of two parts: properties and behaviors

  • Attribute: reflected in the class through member variables (variables outside the method in the class)
  • Behavior: It is reflected in the class through member methods (compared with the previous method, just remove the static keyword)

class definition format

Class definition steps:

①Define class

② Write the member variables of the class

③ Write the member method of the class

public class 类名 {
    // 成员变量
    数据类型 变量1;
    数据类型 变量2;
    …
    // 成员方法
    方法1;
    方法2;
}
复制代码

Sample code:

/*
    手机类:
        类名:
        手机(Phone)
​
        成员变量:
        品牌(brand)
        价格(price)
​
        成员方法:
        打电话(call)
        发短信(sendMessage)
 */
public class Phone {
    //成员变量
    String brand;
    int price;
​
    //成员方法
    public void call() {
        System.out.println("打电话");
    }
​
    public void sendMessage() {
        System.out.println("发短信");
    }
}
复制代码

3.2 Creation and use of objects

how to get object

It is not possible to have a class. We must create an object of the class. There can be thousands of objects of the class, so that the object can represent a specific thing in the real world. So how do you create objects? The format for creating an object is as follows:

类名 对象名 = new 类名();
复制代码

How to use objects

When we create an object, we need to use the properties and behavior of the object: the format is as follows:

使用对象的成员变量:
    对象名.成员变量
​
使用对象的成员方法:
    对象名.成员方法();
复制代码

Sample code:

/*
    创建对象
        格式:类名 对象名 = new 类名();
        范例:Phone p = new Phone();
​
    使用对象
        1:使用成员变量
            格式:对象名.变量名
            范例:p.brand
        2:使用成员方法
            格式:对象名.方法名()
            范例:p.call()
 */
public class PhoneDemo {
    public static void main(String[] args) {
        //创建对象
        Phone p = new Phone();
​
        //使用成员变量
        System.out.println(p.brand);
        System.out.println(p.price);
​
        p.brand = "小米";
        p.price = 2999;
​
        System.out.println(p.brand);
        System.out.println(p.price);
​
        //使用成员方法
        p.call();
        p.sendMessage();
    }
}
复制代码

3.3 Student Objects - Exercises

  • Requirement: First define a student class, then define a student test class, in the student test class to complete the use of member variables and member methods through objects

  • analyze:

    • Member variables: name, age...
    • Membership method: study, do homework...
  • Sample code:

class Student {
    //成员变量
    String name;
    int age;
​
    //成员方法
    public void study() {
        System.out.println("好好学习,天天向上");
    }
​
    public void doHomework() {
        System.out.println("键盘敲烂,月薪过万");
    }
}
​
/*
    学生测试类
 */
public class StudentDemo {
    public static void main(String[] args) {
        //创建对象
        Student s = new Student();
​
        //使用对象
        System.out.println(s.name + "," + s.age);
​
        s.name = "林青霞";
        s.age = 30;
​
        System.out.println(s.name + "," + s.age);
​
        s.study();
        s.doHomework();
    }
}
复制代码

3.4 Default values ​​of member variables

As can be seen from the above object access to member variable properties, member variables can not be given initial values, and member variables actually have default values. The rules for default values ​​are as follows:

type of data Defaults
basic type Integer (byte, short, int, long) 0
float (float, double) 0.0
character (char) '\u0000'
boolean false
reference type array, class, interface null

Guess you like

Origin juejin.im/post/7103407914764730398