Java object-oriented -- method overloading -- construction method -- this keyword ------ (XV)

1, What is an object

Object Oriented Programming:

All objective things are objects, and all things are objects

Any object must have its own characteristics and behavior

→ features: → attributes, usually nouns, represent what the object has

Object

→ behavior: → method, usually a verb, represents what the object can do

2, What is a class

In a group of the same or similar objects, the common features and behaviors are extracted and the concerned parts are retained.

The same class has different characteristic attributes and behavior methods

Properties:

Represented by variables, also known as instance variables

Data type attribute name;          Stringname;         int age;

Attributes are defined inside the class and outside the method

Method:

Represented by method, also known as instance method

public return value type method name (){

/ / body method

                        }

public class Dog {		//Class name
	String name;		//Definition of attributes
	int age;
	String sex;
	String furColor;
	
	public void eat() {		//Define behavior method 1
		System.out.println("eat........");
	}
	public void sleep() {	//Define behavior method 2
		System.out.println("sleep.......");
	}
}

3, Object creation

        

public class Test {
	public static void main(String args[]) {
		Dog dahuang=new Dog();
		
		dahuang.name="chinese rhubarb";		//Access properties, assignment
		dahuang.age=2;			//Object name Attribute name = value;
		dahuang.sex='mother';
		dahuang.furColor="yellow";
		
		dahuang.eat();		//Call method, object name Method name ();
		dahuang.sleep();
	}
}

4, Relationship between objects and classes

Class: defines the characteristics and behaviors that an object should have. Class is the template of an object.

Object: an entity with multiple features and behaviors. An object is an instance of a class.

A class can correspond to multiple object instances

For example, the "dog" class can have many objects, such as object 1: rhubarb, object 2: Wangcai....

Each object has the attributes of the class, and the values of the attributes of each object are different

5, Instance variable

Instance variables will not cause compilation errors because they are not assigned values. They all have their own default values.

Default value of instance variable:

Integer: 0

Decimal: 0.0

Characters: \ u0000 (space)

Boolean: false

Other: null

That is, when the corresponding attribute value of the object is not assigned after the object is created, the output object attribute will display the above default value.

Instance variables exist inside the class and outside the method. They are only valid in this class. Instance variables cannot have the same name as local variables, and local variables take precedence.

        

6, Method of instance

The instance method of an object consists of two parts: the declaration of the method and the implementation of the method

Declaration of method:

What can a delegate do

Composition: modifier , return value type , method name (formal parameter list)

Method implementation:

What to do on behalf of the object: how to implement the corresponding functions at the level

Composition: {logic code}

7, Method overload

In some cases, there may be multiple implementation processes for the same behavior of an object

For example:

There are differences in the "eating" behavior of human objects, and there are differences in the process of eating and taking medicine.

public class Person {		
	
	public void eat(food a) {	
		//Eat food
		//chew
		//Swallow
	}
	public void eat(medicine a) {	
		//Take medicine
		//drink water
		//gulp down
	}
}

Overload: define multiple methods with the same name in a class

requirement:

Same method name

Different parameter lists (different types, numbers and orders of parameters are counted)

It is independent of access modifier and return value type

When calling a method with overload, you need to find the matching method according to the passed in arguments

Advantages: flexible, convenient, shielding use differences

public class Person {	
    //All four are overloaded
	public void eat(){
		
	}	
	public void eat(String food) {	
	
	}
	public void eat(String food,int i) {	
	
	}
    public void eat(int i,String food) {	
	
	}
}

8, Construction method

Construction method:

Class, which is mainly used to create objects.

Features:

The name is exactly the same as the class name

No return value type

When creating an object, it triggers the call of the construction method, which cannot be called manually through a period

Note:

If the defined constructor is not displayed in the class, the compiler provides a parameterless constructor by default.

public class Test {
	public static void main(String args[]) {
		Dog dahuang=new Dog();	//new Dog(); Trigger object creation
		
		dahuang.name="chinese rhubarb";		//Access properties, assignment
		dahuang.age=2;			//Object name Attribute name = value;
		
		
		dahuang.eat();		//Call method, object name Method name ();
		dahuang.sleep();
	}
}
class Dog{
	String name;
	int age;
	public Dog() {
		System.out.println();
	}
}

Object creation process:

Open up object space in memory

Give initial values to each attribute

Execute the code in the constructor

[assign the address of the object to the variable]

        

In the above code block, the address of the object is saved in Dahuang (Reference), and the attributes and methods of the object are accessed through the address in the variable

9, Overloading of construction methods

Construction methods can also be overloaded and follow the overload rules

public class Test {
	public static void main(String args[]) {
		Dog dahuang=new Dog();	//new Dog(); Trigger object creation
		
		Dog wangcai=new Dog("wangcai");//When creating an object, pass in parameters to match the corresponding construction method
		
		Dog huahua=new Dog("huahua",1);//While creating the object, pass the value into the construction method
	}
}
class Dog{
	String name;
	int age;
	public Dog() {
		System.out.println();
	}
	public Dog(String name) {
		System.out.println(name);
	}
	public Dog(String name,int age) {
		System.out.println(name+"--"+age);
	}
}

In a class, if the definition constructor is not displayed, the compiler provides a parameterless constructor by default

If you add constructors manually, the compiler will not provide parameterless constructors by default,

This is to manually add a parameterless construction method according to your own needs.

10, this keyword

Class is a template that can serve all objects of this class; This is the default reference in the class and represents the current instance;

When a class serves an object, this points to the object.

public class Demo {

	public static void main(String[] args) {
		Student s1=new Student();
		s1.sayHi();
		System.out.println(s1);
		Student s2=new Student();
		s1.sayHi();
		System.out.println(s2);
	}

}
class Student{
	String name;
	int age;
	
	public void sayHi() {
		System.out.println(this.name);
	}
}

In the above code block, s1 The this in sayHi () refers to the address space block opened by s1.

                        com.qf.pro2103.day01.Student@15db9742

                        s2. The this in sayHi () refers to the address space block opened by S2.

                        com.qf.pro2103.day01.Student@6d06d69c
When accessing, you can get different name s according to the different points of this.

this usage (I):

Call instance properties and instance methods.

For example: this name        this.sayHi()

public class Demo {
	public static void main(String[] args) {
		Student s1=new Student();
		s1.sayHi();	
	}
}
class Student{
	String name="Luo Xiang";
	int age;
	
	public void sayHi() {
		String name="Zhang San";
		System.out.println(name);
		System.out.println(this.name);
	}
}

In the above code, when the instance variable and the local variable have the same name, the local variable is accessed first;

In this case, if you want to access the instance variable, you need to add this prefix.

this can be omitted if the name is not duplicated

this usage (II):

Call other constructor methods in this class. For example: this() this (argument)

class Student{
	String name;
	int age;
    int id;
	
	public Student() {
		
	}
	public Student(String name,int id) {
		this.name=name;
        this.id=id;
	}
	public Student(String name,int id,int age) {
		this(name,id);       //Call the logic code of the above constructed parameter this name=name; And this id=id;
		this.age=age;
	}
	public void sayHi() {
		System.out.println(name+"--"+age);
	}
}

In the construction method, we can reuse the other construction methods of this class to reuse the logical code for constructing the method.

In the second and third construction methods above, there are redundant codes of name and id

this() calls parameterless construction

This (argument) calls a parameterized construct

Construction must be written on the first line of the constructor

Keywords: Java java-se

Added by sunsun on Sun, 02 Jan 2022 20:35:48 +0200