Java: classes and objects

Keywords: Java

catalogue

Type 1.1

1.1.1 definition of class

1.2 object

        1.2.1 creating objects

            1.2.2 instantiated object

                 1.2.3 creating multiple objects

                1.2.4 problems in object creation   (note)

         1.2.4 differences between basic data variables and reference data type variables

        1.2.5 static variables

object-oriented:

        What is an object: an object is an abstraction of things, and everything is an object (a concrete existence that can be seen and touched).

Type 1.1

        Class is the abstraction and induction of objects. We naturally classify things around us. The principle of classification is abstract, which reflects the essential characteristics related to the current goal, and ignores the non essential characteristics irrelevant to the current goal, so as to find out the commonness of things, classify the things with commonness into one class, and obtain an abstract concept - class.

        Relationship between class and object:

                                Classes: abstract concepts, collections, data types.

                                Object: a specific, individual, instance.

1.1.1 definition of class

        A java source program file is often composed of many classes. From the perspective of users, classes in Java source programs can be divided into two categories.

      

        · System defined classes: Java class libraries. It is a system defined class, and the class library is an important part of the Java language.

        · User defined classes: Although system defined classes implement many common functions, user programs still need to define their own classes according to specific logic for specific problems.

        In Java programs, the format of user-defined classes:

[modifier] class name [extensions parent class name] [list of interfaces implemented by implements class]

{

        Data member

        Member method
}

        The contents enclosed in [] are optional. The keyword class is the beginning of the class definition. The keyword extends means that the class is a subclass, followed by its parent class name, and there is an inheritance relationship between the subclass and the parent class. The keyword implements indicates the interface implemented by the class, followed by a list of interface names. There can be no more than one parent class, and there can be multiple interfaces.

        Modifiers include:

  1. Public access controller: indicates that this class is a public class, and its data members and member methods can be accessed by any other class.
  2. Default access control character: that is, no access control character is written, which means that this class can only be accessed by classes in this package and cannot be introduced using the import statement
  3. Abstract modifier: indicates that the class is abstract, that is, it cannot be instantiated, that is, it cannot be used to create objects. The main purpose of defining it is to use it to create word classes.
  4. Final modifier: indicates that the class is the final class, that is, it cannot be used to create subclasses.

Data member (attribute): used to indicate the static characteristics of a class.

Member method: used to indicate the dynamic behavior of the class.

//Description of the person class
class Person{                //Class indicates that this is a class and Person is the class name
    String name;            //full name
    int height;             //height
    int weight;             //weight
    public void run(){      //Define a ren() method
    ...
    }
    public void play(){     //Define a play() method
    ...
    }

}

        In the above code, name, height and weight are data members  ,

                              public void run() {} and public void play() {} are member methods.

1.2 object

        Class is the template of object, and object is the instance of class. After defining class, you can use class to create object. Creating objects includes the processes of declaring objects, creating objects, and initializing objects. After creating the object, you can use the object.

        1.2.1 creating objects

                Class name. Object name = new constructor ();

            1.2.2 instantiated object

                        Calls to properties and methods:

                        Object name. Properties

                        Object name. Method

                Properties:

                        Default values are assigned.

                        The data default value of the reference type is null

                        int defaults to 0          

                        The default value of char is null character

                        The default value of double is 0.1

                        Boolean defaults to false

The code is as follows:

public class Person {
	String name;
	int height;
	int weight;
	public void run() {
		System.out.println("run..");
	}
	public void play() {
		System.out.println("Write code..");
	}
	
}
public class Test {

	public static void main(String[] args) {
		Person p = new Person();//Create an object with the name p
		
		System.out.println(p.height);
		p.height = 170;	//Assign a value to the attribute height
		System.out.println(p.height);//Copy before exporting
		
		System.out.println(p.weight);
		p.weight = 120;//Assign a value to the attribute weight
		System.out.println(p.weight);//Copy before exporting
		
		System.out.println(p.name);
		p.name = "Zhang San";//Assign a value to the attribute name
		System.out.println(p.name);//Copy before exporting
		
		p.play();
		p.run();
		

	}

}

        Because the attribute is a value, the attribute should be output, otherwise the compiler will report an error.

        The above program contains two classes, in which Test is the main class, and its only purpose is to Test the class Person. When the program is run, the Java running system will call the main method of the main class. (the main method is the entrance to the program running. A program can only have one main method.)

        You can put two classes in the same file, but only one class (the class with the main method) in the file is a public class. In addition, the public class must have the same name as the file.

The execution results are:

                 1.2.3 creating multiple objects

                         Create the calling class Test1 and call Person. The code is as follows

public class Test1 {

	public static void main(String[] args) {
		Person p = new Person();
		Person p1 = new Person();
		Person p2 = new Person();
		
		p.height = 168;
		p.weight = 120;
		p.name = "Lisa";
		p.run();
		System.out.println("My name is:"+p.name);
		System.out.println("My height:"+p.height);
		System.out.println("My weight:"+p.weight);
		System.out.println();	//Line feed (for better viewing)
		
		p1.height = 170;
		p1.weight = 122;
		p1.name = "Li Hua";
		p1.run();
		System.out.println("My name is:"+p1.name);
		System.out.println("My height:"+p1.height);
		System.out.println("My weight:"+p1.weight);
		System.out.println();	//Line feed (for better viewing)
		
		p2.height = 180;
		p2.weight = 130;
		p2.name = "Monkey D Luffy";
		p2.run();
		System.out.println("My name is:"+p2.name);
		System.out.println("My height:"+p2.height);
		System.out.println("My weight:"+p2.weight);
		System.out.println();	//Line feed (for better viewing)
	}

}

Execution results:

                1.2.4 problems in object creation   (note)

        When creating an object, the keyword new is used to explain that a new space is opened up in the heap. Code demonstration:

public class Test1 {

	public static void main(String[] args) {
		Person p = new Person();	//A space is opened up in the heap, and p stores the address of this space
		Person p1 = new Person();	//A space is opened up in the pile, and p1 stores the address of this space
		Person p2 = p;		//The address of p is passed to p2. p2 and p point to the same memory in the heap.
		
		p.name = "Wang Wu";
		p1.name = "Three children";
		System.out.println(p.name);
		System.out.println(p1.name);
		System.out.println(p2.name);	//p2 was not previously assigned a value
		System.out.println(); //Line feed
		
		p2.name = "Four children";
		System.out.println(p.name);
		System.out.println(p1.name);
		System.out.println(p2.name);
		
	}

}

      Execution results:

Illustration:

 

         1.2.4 differences between basic data variables and reference data type variables

        Each variable represents a memory location for storing values. When a variable is declared, it tells the compiler what type of value this variable can store. For basic type variables, the value stored in the corresponding memory is the basic type value. For application type variables, the corresponding memory stores only a reference, which is the address of the object.

        When a variable is assigned (=) to another variable, the actual value of the variable is assigned to another variable for the basic data type. For reference data types, the address stored in one object name is assigned to another object name (two object names point to one object together). The assignment principle of reference type variables is the same as the above figure.

        1.2.5 static variables

        Static variables are shared by all objects in the class. Static methods cannot directly access instance members in a class (they can be accessed by creating objects). Static is also called unique.

        If an object changes the value of the podium variable, all objects of the same class will be affected. Java supports static methods and static variables that can be accessed without creating an instance (through class name. Static properties / static methods).

        The code is as follows:

public class Demo {
	static int x = 5;//Define the static variable x of type int and assign a value of 5
	public static void run() {
		System.out.println("Running...");
	}
}

    Create test class

public class Text {
	public static void main(String[] args) {
		Demo d = new Demo();
		Demo d1 = new Demo();
		d.x = 10;
		System.out.println(d.x);
		System.out.println(d1.x);
		
		System.out.println(Demo.x);
		
		Demo.run();
	}
}


The implementation results are as follows;

        

Posted by cableuser on Wed, 01 Dec 2021 17:39:44 -0800