Java basic classes and objects

Posted by infestedarch0n on Sun, 26 Sep 2021 20:01:34 +0200

Classes and objects

object
Objects in Java are similar to objects in reality, with States and behaviors described.

Realistic objectSoftware object
Example: Peopleobject
Status: name, heightProperties (member variables)
Behavior: study, workEmbodied by method (member method)

class
A class is an abstraction of an object. A class is a template that describes the common behavior and state of a group of objects. Class name should be capitalized
Class variable:
Class variables are also declared in the class, outside the method body, but must be declared as static type.
Access modifier for class:
The default, also known as default, is visible in the same package without any modifiers.
Private, specified with the private modifier, visible within the same class.
Common, specified with the public modifier, visible to all classes.
Protected, specified with the protected modifier, is visible to classes and all subclasses in the same package.
***Function: * * * can be used to protect access to classes, variables, methods and constructor methods.
Content:
1. Define member variables. Describe the characteristics of the object (generally, unassigned variables (not initialized) are called declaration variables, and assigned variables are called definition variables)
2. Define member methods. Describes the behavior of an object.
Class construction method
1. Note: the name of the method must have the same name as the class. The method name cannot be preceded by a return value type declaration. Return cannot have a return value. You can write only one return to end.
2. Two construction methods:
The area and perimeter of a rectangle are calculated, and the nonparametric construction and parametric construction are used to understand how to construct the method.
Nonparametric Construction:

Create a Damo001 class.

/*
 * Nonparametric structure
 * 1,First encapsulate the member variable
 * 2,Parameterized | nonparametric construction method
 * 3,Write member method
 */
package com.itheima01;

public class Damo001 {
	private int  length;
    private int  width;
    
    public class Damo001 {
}//Null parameter constructor, if we don't write any constructor ourselves. The system will provide a parameterless construction method by default. For us to create objects.
    public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;//This keyword indicates a member variable. If this is not written, it is a local variable
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int  getPerimeter(){
        int c = (length+width)*2;
		return c;
    }
	public int  getAcreage(){
        int d = length*width;
		return d;
    }
	
 } 
 

Write out the main () function, create an object, call the set function through the object, assign a value, and call the method.

package com.itheima01;

import java.util.Scanner;

public class Qiu Area perimeter {
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		System.out.println("Enter length and width");
		int a =sc.nextInt();
		int b =sc.nextInt();
		Damo002 g=new Damo002();
		g.setLength(a);
		g.setWidth(b);
		System.out.println("the measure of area"+g.getAcreage()+"Perimeter"+g.getPerimeter());
	}

}

Parametric structure:

Create a Damo002 class.

/*
 * Parametric structure
 * 1,First encapsulate the member variable
 * 2,Parameterized | nonparametric construction method
 * 3,Write member method
 */
package com.itheima02;
public class Damo002 {
	private int length;
	private int width;
	public class Damo002 {
}//Empty parameter construction method. Parameter construction has been written in Damo002. The system will not provide parameter free construction method. Objects cannot be created with empty parameters. Write one to facilitate use and avoid errors.
	public Damo002(int length, int width) {//Method overloading is used
		
		this.length = length;
		this.width = width;
	}
	
	public int  getPerimeter(){
        int c = (length+width)*2;
		return c;
    }
	public int  getAcreage(){
        int d = length*width;
		return d;
    }

}

Write out the main () function, create the object, assign values directly when creating the object, and call the method

package com.itheima02;
/*
 * Create the object before calling.
 */

import java.util.Scanner;

public class Qiu Area perimeter {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter length and width");
		int a = sc.nextInt();
		int b = sc.nextInt();
		Damo001 g =new Damo001(a, b);//Create a new instance object with a parametric construction method
		System.out.println("the measure of area"+g.getAcreage()+"Perimeter"+g.getPerimeter());
	}


}

this keyword
1. Solve the conflict between member variables and local variables

public class Text01 {
	int age;					//Member variable age
	public Text01(int age) {	//Local variable age
		this.age  = age;		//this.age refers to the member variable. If this is not written, it is a local variable
	}
}

2. Calling member methods with the this keyword
Generally, there is no need to write for calling, because the system defaults to [this.] called method name (); This. Can be omitted.
3. Call the constructor through the this keyword
Other construction methods (in this class) can only be called in the form of this([parameter 1], [parameter 2]...), and cannot be called arbitrarily.
It can only be used in construction methods to call other construction methods, and the construction method called through this must only be written in the first article of the method and can only appear once.
You cannot call each other with this in two constructor methods of a class.

Write a test class to illustrate

package com.itheima01;

import java.util.Scanner;

public class Text01 {
	//Write a mobile phone class
	//Member variables - color, price
	private String color;
	private int price;
	//Create an empty parameter constructor
	public Text01() {
		
	}
	//Create a parameterized constructor
	public Text01(String color, int price) {
		this.color = color;
		this.price = price;
		System.out.println("colour"+color+"Price"+price);
	}
	
	public static void main(String[] args) {
		Text01 t = new Text01("blue", 3000);
		
	}
	
}

Here, it is directly assigned through the normal parametric structure

Then we will use this([parameter 1], [parameter 2]...] in the form of parameter free construction to call the parametric structure.
In the main() function, modify the way to create objects without parameter construction.

package com.itheima01;

import java.util.Scanner;

public class Text01 {
	//Write a mobile phone class
	//Member variables - color, price
	private String color;
	private int price;
	//Create an empty parameter constructor
	public Text01() {
		this("red", 2000);
	}
	//Create a parameterized constructor
	public Text01(String color, int price) {
		this.color = color;
		this.price = price;
		System.out.println("colour"+color+"Price"+price);
	}
	
	public static void main(String[] args) {
		//Text01 t = new Text01("blue", 3000);
		Text01 t = new Text01();
	}
	
}

It is found that the call is successful in the no parameter construction method.

Topics: Java