java object-oriented encapsulation, inheritance, polymorphic cases

Person parent class

public class Person {
    private String name;
    private int age;

    public Person() {
        super();
    }   
    public Person(String name, int age) {
        super();
        setName(name);
        setAge(age);
    }

    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 > 0 && age < 150){
            this.age = age;
        }
        else{
            System.out.println("Age is not reasonable!!!");
        }
    }
    public void show(){
        System.out.println("I am" + getName() + ",This year" + getAge() + "Age!");
    }
    public static void test(){
        System.out.println("Person Static methods in classes");
    }

}

Student subclass inherits Person parent class

public class Student extends Person {
    private int id;

    public Student() {
        super();
    }

    public Student(String name, int age,int id) {
        super(name, age);
        setId(id);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        if(id > 0){
            this.id = id;
        }else{
            System.out.println("Unreasonable number!!!");
        }
    }

    @Override
    public void show(){
        super.show();
        System.out.println("My number is: " + getId());
    }
    public static void test(){
        System.out.println("Person Static methods in classes");
    }
}

Test Class

public class TestPersonStudent {

    public static void main(String[] args){
        //Use parent class references to father's own objects
        Person p = new Person("zhangfei", 30);
        //Call the Person class's own show() method
        p.show();

        System.out.println("----------------------");

        //Use the subclass's own reference to point to the subclass's own object
        Student s = new Student("guanyu", 35, 1001);
        //When there is no show() method in the subclass, the show() method in the parent Person is called
        //When the subclass overrides the show method, the subclass Student's own method is called
        s.show();

        System.out.println("----------------------");
        //Use parent class references to subclass objects to form polymorphisms
        //Student type to Person type conversion, small to large range conversion, automatic type conversion
        Person ps = new Student("liubei", 40, 1001);
        //Resolution: Call the show() method of the Person class at compile time and the show method of Student at run time
        ps.show();

        System.out.println("-----------------------");

        //Use ps to invoke non-static methods for testing
        //ps is the Person type's own reference, so you can call the Person type's own method
        String str = ps.getName();
        System.out.println("Name obtained is: " + str);//liubei

        //ps.getId(); error

        System.out.println("-----------------------");
        //Use ps to invoke static methods for testing. Static methods recommend using class names.
        //ps.test();
        Person.test();

        System.out.println("-----------------------");
        //How do I implement method calls in subclasses?
        //Person type to Student type conversion, large=>small, forced type conversion required
        //Target type variable name = (Target type) variable name of source type;
        Student st = (Student)ps;
        int res = st.getId();
        System.out.println("res = " + res);

        //String sr = (String)ps; error
        //Teacher t = (Teacher)ps; //Compile pass, runtime type conversion exception
        /* If modified
         * Person dd = new Teacher();  //Use a parent class reference to point to a child class's object so it's correct
         * Teacher t = (Teacher)dd; 
         * int ee = t.getSex();
         * System.out.println("Gender is "+ee";
         * */       
        //Determines whether the object type that ps actually points to is of Teacher type, returns true if it is, otherwise false
        if(ps instanceof Teacher){
            System.out.println("You can safely cast...");
        }else{
            System.out.println("Cannot cast!");
        }

    }
}

Teachers

package xdl.day09;

public class Teacher extends Person {
/*    private int sex;

    public Teacher(){
        super();
    }
    public Teacher(int sex) {
        super();
        setSex(sex);
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }
    public void show(){
        super.show();
        System.out.println("My gender is "+getSex()";
    }
  */  
}

Polymorphism:
Polymorphism refers to the multiple forms that the same thing displays.
For example:
Drinks: Coke, Sprite, Lehu, Pulse...
Graphics: Rectangle, Circle, Triangle...
Integer: byte b= 10; short s = 10; int i = 10;...

Polymorphic grammar formats:
Parent type reference = new subclass type ();
For example:

Person p = new Student();
p.show();

Resolution:
In the compilation phase p is of Person type, so the Person class's own show() method is called, and if not, compilation errors occur.
The object that p really points to at runtime is the Student type, so you end up calling your own show() method in the Student class.

Effect of polymorphism
(1) For parent class references to subclass objects, only methods of the parent class can be called directly at the compilation stage, not subclasses.
(2) For non-static methods that both parent and child classes have, the overridden version of the subclass is finally invoked;
(3) For both parent and child static methods, the final call to the version in the parent is independent of the type of object being pointed to;

Conversion between reference types
(1) Conversion between reference types must occur between parent and child classes, which are divided into automatic type conversion and forced type conversion;
(2) Automatic type conversion is required for the conversion of subclass type to parent type.
The conversion from parent type to child type requires a cast to occur.
(3) Compilation errors occur if there is no strong transition in parent-child relationship, and compilation passes if there is a strong transition in parent-child relationship.
If the target type is not the type that the reference actually points to, a type conversion exception occurs at run time.
(4) To avoid the above errors, it is usually only necessary to make a judgment on the basis of a cast in the following format:

        if(Reference variable name instanceof data type){
            System.out.println("You can safely cast...");
        }else{
            System.out.println("Cannot cast!");
        }
    //-Determines whether the object to which the reference refers is of the specified data type, returns true if it is, or false if it is not;

The Meaning of Polymorphism
You can shield the differences between different subclasses and write generic code to produce different effects.

Posted by ProblemHelpPlease on Mon, 20 May 2019 12:48:53 -0700