Java learning (classes and objects)

Class definition

An aggregate with the same or similar properties, which is abstract, invisible and untouchable

Classes allow you to create objects

Syntax format

class name{
/ / feature 1
/ / feature 2
    
/ / behavior 1
/ / behavior 2
}

object

The object is real and has multiple attribute values

A class can create multiple objects

A group of objects or an object is abstracted into a class

Use the new keyword to create an object or group of objects.

encapsulation

Privatize properties. The privatized properties cannot be accessed and used by other classes

At the same time, a public setter method is provided to set the value of the private property

Filter and mask codes can be written inside the method, so that illegal values cannot be assigned

Method can write additional filtering code relative to attributes to make the data legal

advantage:

Improve program security

Disadvantages:

Too much code

eg: create a Student class and encapsulate the attributes

public class Student {
	//Encapsulation properties
	private String name;
	private int age;
	private String address;
	private String zipCode;
	private String mobile;
	public void setName(String name){
		this.name = name;
	}
	
	public void setAge(int age){
		this.age = age;
	}
	public void setAddress(String address){
		this.address = address;
	}
	public void setZipCode(String zipCode){
		this.zipCode = zipCode;
	}
	public void setMobile(String mobile){
		this.mobile= mobile;
	}
	public String getPostAddress(){
		return "address:\t" + address + "\nzipCode\t" +zipCode; 
	
	}
	}

Create an object called Xiaoming, calling attributes from Student.

public class xiaoming {public static void main(String[] args) {
	Student s = new Student();
	
	s.setName("xiaoming");
	s.setAge(13);
	s.setAddress("xian");
	s.setZipCode("723200");
	s.setMobile("54264723467");
	
	System.out.println(s.getPostAddress());
	
}

}

inherit

Understanding (son inherits father)

The subclass can use all non private methods defined by the parent class

Use the extends keyword to indicate that the preceding class is a subclass of the following class.

Use the super keyword to call directly defined properties and methods in the parent class.

eg: parent class (which defines the radius of the circle, the length and width of the rectangle, and the method of calculating the circumference and area of the circle and rectangle)

public class Shape {
	 private double banjing;
	 int chang;
	 int kuan;
    public void setR(double bangjing) {
		this.banjing=bangjing;
	}
	
	public void zhouchang1(){
		double c;
		c=Math.PI*2*banjing;
		System.out.println("Circumference of circle:" + c);
	}	
	public void mianji1(){
		double s;
		s=Math.PI*banjing*banjing;
		System.out.println("Area of circle:" + s);
		
	}
	
		public  void zhouchang2(){
			int c;
			c=2*(chang + kuan);
			System.out.println("Perimeter is:" + c);
		}
		public void mianji2(){
			int s;
			s=chang*kuan;
			System.out.println("The area is:" + s);
		}
}

Subclass: circle (directly call the method to calculate the circumference and area of the circle through the super keyword)

public class Circle extends Shape{
	
	public void zhouchang1(){
		super.zhouchang1();
		}
	public void mianji(){
		super.mianji1();
		}	

}

Subclass: rectangle (call the method in the parent class to calculate perimeter and area)

public class Rect extends Shape {
    public void zhouchang2(){
		super.zhouchang2();
		}
	public void mianji(){
		super.mianji2();
		}
	
}

public class textshape {
	public static void main(String[] args) {
		
	Circle r =new Circle();
	
	r.setR(3);
	r.zhouchang1();
	r.mianji1();
	
	
	Rect l = new Rect();
	l.chang=6;
	l.kuan=5;
	l.zhouchang2();
	l.mianji2();
}

}

Keywords: Java

Added by MadnessRed on Sat, 01 Jan 2022 10:32:56 +0200