Java basics-02 three characteristics of object-oriented

Object-oriented

Java is an object-oriented language, so before you formally learn java, let's first understand what is object-oriented?

Object-oriented refers to a paradigm of programming, as well as a method of programming. The object is the concrete realization of the class. It takes objects as the basic unit of the program and encapsulates the program and data to improve the reusability, flexibility and scalability of the software. Everything is an object. Through an object-oriented way, it abstracts the things in the real world into objects. Reality The relationships in the world are abstracted into classes and inherited to help people achieve abstraction and digital modeling of the real world.

 

Three characteristics of object-oriented: encapsulation, inheritance, polymorphism

Encapsulation refers to the privatization of attributes, the purpose is to enhance data security, not allowing other users to access and modify data at will, simplify programming, users do not need to care about the specific implementation details, but only through the external interface to access the members of the class

  • Provide setter and getter methods to access properties as needed
  • Hide specific attributes and implementation details, and only open interfaces to the outside world
  • Control the access level of attributes in the program

For example, the following code:

In the User class, three member variables are defined, namely idCard, name, and user. Their access modification is private, and these variables are set and valued through the setter and getter methods.

public class User {
	private String idCard;
	private String name;
	private User user;
	
	public String getIdCard() {
		return idCard;
	}
	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}	

}

Inheritance refers to the construction of a new class (subclass) on the basis of an existing class (parent class), the subclass can access and use the non-private member variables in the parent class, and rewrite the non-private members in the parent class method. But the private member variables and methods in the parent class cannot be accessed and called). The role of inheritance is to improve the reusability of the code.

  • A class in java can only inherit one parent class, and can only inherit access rights non-private attributes and methods
  • The subclass can override the method in the parent class and name the attribute with the same name in the parent class

For example, the following code defines a private variable height, a public variable weight, a private method showHeight() and a public method showWeight() in the parent class respectively;

Called and rewritten separately in the subclass, private variables and methods are not accessible by the subclass, the subclass only has access to one public variable and method, and the subclass overrides the public method showWeight() of the parent class;

father:


public class User {
	private String idCard;
	private String name;
	private int height=180;//父类私有变量
	public int weight=90;//父类公有变量
	private User user;
	
	public String getIdCard() {
		return idCard;
	}
	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	//父类私有方法
	private void showHeight() {
		System.out.println("user showheight:"+height);
	}
	//父类公有方法
	public void showWeight() {
		System.out.println("user showheight:"+weight);
	}
	
}

Subclass:


public class Women extends User{
	@Override
	public void showWeight() {
		System.out.println("user showheight:子类的体重为"+weight);
	}

	public static void main(String[] args) {
		Women women=new Women();
//		women.height=150; //子类对象对父类的私有成员变量使用报错
		women.weight=88;//子类对象对父类的公有成员变量使用正常
//		women.showHeight();	//子类对象调用父类的私有方法使用报错
		women.showWeight(); //子类对象调用父类的私有方法使用正常
	}
}


Polymorphism can be divided into two types: polymorphism at design time and polymorphism at runtime

Design (compile) polymorphism: Overload, which means that Java allows the same method name but different parameters (the return value can be the same or different), and one or more functions with the same name are allowed in the same class. As long as the parameter type or number of parameters is different

Runtime polymorphism: Override must be in the inheritance system. The subclass overrides the parent class method, and the JVM runtime determines which method to call according to the type of the method called.

The code is shown in the following example

public class Women extends User{
	int CurrentWeight;
	
	//重写showWeight方法
	@Override
	public void showWeight() {
		System.out.println("user showheight:子类的体重为"+weight);
	}

    //重载showWeight方法
	public void showWeight(int CurrentWeight) {
		System.out.println("当前体重为"+CurrentWeight);
	}

	public static void main(String[] args) {
		Women women=new Women();
//		women.height=150; //子类对象对父类的私有成员变量使用报错
		women.weight=88;//子类对象对父类的公有成员变量使用正常
//		women.showHeight();	//子类对象调用父类的私有方法使用报错
		women.showWeight(); //子类对象调用父类的私有方法使用正常
		
		women.showWeight(66);//重载的方法
	}

}

 

 


 

 

 

Guess you like

Origin blog.csdn.net/dream_18/article/details/115051472