Classes, objects, encapsulation, inheritance and polymorphism in Java (very detailed)

1. Class and object:

(1) A class is an abstract data type, which is an overall description or definition of a certain type of transaction, but it does not represent a specific transaction. For example: people (people have characteristics such as name, age, gender, etc.); objects are concrete examples of abstract concepts. For example: Zhao Yun is a concrete instance of human (kind), and Zhao Yun's wealthy wealth is a concrete instance of dog (kind). Objects are concrete examples that are presented, not abstract concepts.
(2) Creation of classes and objects:

//这里就是创建了一个Person类
public class Person(){
    
    
	//每个类中又包含属性以及方法
	String name;
	int age;
	//方法
	public void study(){
    
    
		System.out.println(this.name+"人在学习");
	}
}

Instantiation of the class

public class Application(){
    
    
	public static void main(String[] args){
    
    
		Person TW=new Person();//先用new创建对象,这里TW这个对象就是Person类的实例化
		TW.name="小明";//在没有赋值之前姓名默认为null,年龄默认为0
		TW.age=21;
		//输出这个人的姓名以及年龄
		System.out.println(TW.name);
		System.out.println(TW.age);
	}
}

(3) Constructor: The constructor in the class is also called the constructor method, which must be called when creating the object. The default initialization and the call to the constructor in the class are performed. The method name must be the same as the class name. There are return value types, including void can not be written

public class Person(){
    
    
	String name;
	int age;
	//构造一个无参构造器,并且进行实例化的初始化值
	public Person(){
    
    }
	//这里一旦定义了有参构造器,在使用new来创建对象时无参构造器必须存在,否则就会报错
	public Person(String name,int age){
    
    
		this.name=name;
		this.age=age;
	}
}
public class Application(){
    
    
	public static void main(String[] args){
    
    
		Person TW=new Person("小明"21);//传参数
		System.out.println(TW.name);
		System.out.println(TW.age);
	}
}
	

2. Package

Generally, direct access to the actual display of the data in an object should be prohibited, but should be accessed through the operating interface. This process is called encapsulation, and encapsulation can increase data security

public class person {
    
    
	//当属性进行私有以后,外部的就无法对属性进行访问,此时就要在内部获得get和set方法
    private String name;
    private int age;
    private int id;
	/*一下就是获得每个属性的get和set方法,get方法是获取值,set方法则是设置值
	我是在vscode上来写的,快速生成get和set的方法为鼠标右键,然后选择源代码操作,
	然后选择Generate Getters and Setters,选中要生成get和set方法的属性即可自动生成*/
	public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
    //这里可以自己对年龄加一个限制,可以过滤掉不合法的数据
    	if(age>120||age<0){
    
    
    		this.age = 2;
    	}else{
    
    
    		this.age = age;
    	}
    }

    public int getId() {
    
    
        return id;
    }

    public void setId(int id) {
    
    
        this.id = id;
    }
}
public class Application(){
    
    
	public static void main(String[] args){
    
    
		Person TW=new Person();
		TW.setName("小明");//修改信息用set方法
		System.out.println(TW.getName());//获取信息用get方法
		TW.setAge(999);//这里传入的就是一个不合法的数据,根据判断条件就会输出2
		System.out.println(TW.getAge());
	}
}

3. Inheritance

Inheritance is actually very common in life. For example, a son inherits the characteristics of his parents. In addition, there is only single inheritance in Java and no multiple inheritance. Inheritance is the relationship between class and class.

//父类
public class Person(){
    
    
	public String name="TWQ";//如果这里改成private则这个属性就是父类独有的,子类就不可以继承此属性
	public int age=23;
	public void say(){
    
    
		System.out.println("世界那么大我想去看看");
 	}
}
//子类
public class student extends Person{
    
    
	
}
public class Application(){
    
    
	public static void main(String[] args){
    
    
		student TW=new student();
		//student中原本没有age和say方法,但由于继承则也会有父类的所有属性以及方法
		System.out.println(TW.name);
		System.out.println(TW.age);
		Tw.say();//这里会输出父类中say方法内部的输出
	}
}

4. Polymorphism

That is, the same method can adopt a variety of different behaviors according to the different sending objects; the actual type of an object is determined, but there are many types of object references that can be pointed to; in addition, polymorphism is the polymorphism of methods, The attributes are not polymorphic.

public class Person(){
    
    
	public void TWQ(){
    
    
		System.out.println("son");
	}
}
public class student extends Person{
    
    
	//这个方法用来说明main中第二次调用,第一次调用的时候没有这个
	public void TWQ(){
    
    
		System.out.println("eat");
	}
}
public class Application(){
    
    
	public static void main(String[] args){
    
    
		//下面三个即为多态的过程,即同一个学生类用不同的状态表示
		student t1=new student();
		Person t2=new studnet();//父类的引用指向子类对象
		Object t3=new student();//Object又称为祖宗类
		t2.TWQ();//由于子类继承父类方法,所以t2可以按照父类方法进行输出,同理t1有人同样可以输出,显而易见父类不能继承子类的方法
		//如果在子类student中重写父类中的方法,则在运行方法时,都按照子类的来
		t1.TWQ();//此时这俩输出的结果都是按照子类的方法来的,也就是输出eat
		t2.TWQ();
	}
}

Guess you like

Origin blog.csdn.net/weixin_44313771/article/details/107086434