Java summary Java object, method overload, construction method

Defining java objects

  1. Object declaration: class name object name;
  2. Object creation: object =new class name ()// New allocates memory space for the object.
    That is, merge 12 into class name object name =new class name (); For details, see the examples in the introduction below.
    Example: Computer c=new Computer(); Where C is an object of entity class Computer.

Method overloading

Overloading: a method in java that appears to facilitate method naming
(1) Same method name
(2) Different parameter lists (different numbers; same number, different types of parameters)
(3) Parameter names are not considered

//Computer entity class
public class Computer{
	public int add(int a,int b){//Method 1
		return a+b;
	}
	public double add(double a,int b){//Method 2
		return a+b;
	}
	public int add(int a,int b,int c){//Method 3
		return a+b+c;
	}
}
//Main class
public class test{
	public static void main(String[] args) {
		Computer c=new Computer();//Create an object of entity class Computer
		System.out.println(c.add(1,2));//Call method 1
		System.out.println(c.add(1.2,1));//Call method 2
		System.out.println(c.add(1,2,3));//Call method =3
	}
}

Through the above example, we can observe which method the class object calls: it depends on (1) the number of transmission parameters (2) the data type

java constructor

  1. Why is there a construction method? Facilitate the assignment of member variables.
  2. Creation of construction method: similar to the following construction
Access control character class name([parameter list]){
}

Note:
(1) Constructor has no return value
(2) Constructor cannot use static and final modifiers

  1. There will be no parameter constructor in the class by default, but after explicitly customizing the constructor, the default constructor parameters in the class will be hidden. (therefore, when nonparametric construction parameters need to be used, and the parametric construction method happens to be in the class, the nonparametric construction parameters need to be written manually.)
//Computer entity class
public class Computer{
	int number;
	int price;
	//Nonparametric construction parameters
	public Computer() {	
	}
	//Parametric construction parameters
	public Computer(int number,int price) {	
		this.number=number;
		this.price=price;
	}
	public void print() {
		System.out.println("Total number of computers:"+number+"´╝îComputer unit price:"+price);
	}
}
//Main class
public class test{
	public static void main(String[] args) {
		Computer c=new Computer();//Create an object of entity class Computer
		c.print();
		c=new Computer(2,2400);
		c.print();
	}
}
Results:
Total quantity of computers: 0, unit price of computers: 0
 Total quantity of computers: 2, unit price of computers: 2400

From the above example, the parameterless construction parameter is used for the Computer object for the first time. It can be seen that the default value is output when using the default construction parameter. When assigning a value again, the parametered construction parameter is called, and the output value is the corresponding value stored in memory.

4. multiple constructors can be constructed in a class. Constructors can also call each other through this([argument list]), but they are placed in the first line of valid code.

	//This part of the code only explains how to use it. Under normal circumstances, it can be constructed separately.
	public Computer(int number,int price) {	
		this.number=number;
		this.price=price;
	}
	//When some variables, such as location, are added to the Computer class, and the construction method containing location is constructed again, the construction parameters that have been assigned values for quantity and price can be called in this construction method.
	public Computer(int number,int price,int palce) {	
		this(number,price);
		this.place=place;
	}

Posted by baronmask on Tue, 31 May 2022 05:51:22 +0530