Packaging and Constructor

Posted by pagedrop on Tue, 14 May 2019 20:12:09 +0200

Lesson Eight                            2018-04-23  02:07:01

Encapsulation and Hiding:
Problem: When an object of a class is created, it may come as an unexpected failure to satisfy the actual situation if the corresponding object property is assigned directly through the way of Object.Attribute.

Lead out: Control object access to attributes through the form of "object.method".Property requirements can be represented in a way.

Solution: 1. Privateize the properties of the class, 2. Provide a public method (setter & getter) to implement the call.That is, encapsulation.

class Person {
//    Privateize member properties
	private String name;
	private int age;
//    1. Privateize the properties of the class and 2. Provide a public method (setter & getter) to implement the call.
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public int getAge() {
		return age;
	}
//    Conditional Judgment Control Data Writing
	public void setAge(int age) {
		if (age > 0 && age < 130) {
			this.age = age;
		} else {
//			System.out.println("incorrect input");
			throw new RuntimeException("Input Error");
			
		}
	}
}

 

Permission modifier:

Private (Inside Class)
Default (inside class, same package)
Prtected (inner class, same package, subclass)
Public (inside class, same package, subclass, anywhere)

1. The above permissions range from small to large and can modify attributes and methods
2. Permission modifiers for class es can only have public and default

Figure:

 

Constructor (constructor, construction method)

1. The role of the constructor: 1. Creating objects 2. Assigning values to the attributes of the created objects

1. When designing a class, the program defaults to providing an empty parameter constructor if the class's constructor is not explicitly declared
2. Once the class constructor is explicitly defined, the default constructor is no longer available
3. How to declare the constructor of a class.Format: Permission modifier class name (parameter) {}
4. Overload between multiple constructors of a class

2. Order of attribute assignment for class objects:
1. Default Initialization of Properties (JDK AutoComplete)
2. Explicit initialization of attributes (manual)
3. Initialize attributes through a constructor (manual)
4. Assign attributes by "object-to-method" (manual)

3. Note:

 

1. When creating an object, the constructor (construction method) is called on its own
2. There can be multiple, differentiated by parameter type and parameter list
3. The constructor is like a special method, which differs from the method as follows:
3.1 Constructor name must be the same as type, method does not need
3.2 Constructor does not allow return values, method must have return values If it does, it is equivalent to normal method
3.2 Constructor cannot be invoked by method, method is called by object

 

public static void main(String[] args) {
		// TODO Auto-generated method stub	
		Person p1 = new Person();
		Person p2 = new Person("fkuennhvo");
		Person p3 = new Person("fkuennhvo",24);
	
		
		System.out.println(p1.getName());
		System.out.println(p2.getName()+p2.getAge());
		System.out.println(p3.getName()+p3.getAge());
	}

}

class Person{
	//attribute
	private String name;
	private int age;
	
	//constructor
	public Person(){
		
	}
	public Person(String n){
		name = n;
	}
	public Person(String name , int age){
		this.name = name;
		this.age = age;
	}
	
	//Method
	public void setName(String n){
		name = n;
	}
	public void setAge(int a){
		age = a ;
	}
	public String getName(){
		return name;
	}
	public int getAge(){
		return age;
	}
}

 

There is an unsolved problem:!

 

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Scanner scanner = new Scanner(System.in);
		TestTriAngle t = new TestTriAngle();

		int S = t.S;
		// Why the final result is still 0!!!
		System.out.println(S);
	}
}

// Get the width (K) and height (C) of a triangle

class TriAngle {
	private int C, K;

	public int getC() {
		return C;
	}

	public void setC(int c) {
		C = c;
	}

	public int getK() {
		return K;
	}

	public void setK(int k) {
		K = k;
	}

	// Construction with parameters, receiving width and height
	public TriAngle(int C, int K) {
		this.C = C;
		this.K = K;
	}
}

// Used to calculate the area of a triangle
// S = C*K/2
class TestTriAngle {
	int S;

	public void Tri() {
		// Create a parametric constructor and pass in the width, up.
		TriAngle T = new TriAngle(3, 4);
		this.S = T.getC() * T.getK() / 2;
		// Question, isn't this.S pointing to that member variable?
	}
}

Topics: Java Attribute JDK