java polymorphism and encapsulation

Package

1. What is the packaging:
hidden attributes (fields) to provide a common method (getter, setter) available for external access
2, to achieve: the privatization by private key attribute, and then accessed through getter and setter methods
3, this property, method. Enough to create devices. Call the properties of the current class, method, constructor when calling the constructor is this. To the top row

Enough to create device
Syntax:
permission modifier method name (parameter) {

}
Effect:
1, to create the object
2, when creating objects can be property assignment
create enough to create's Rule
1, when the class is created by default automatically create a free parameter enough production method
2, when manually add a enough to create an , the system does not provide a default method of making it be a
3, constructor method name and class names consistent
4, the permissions for the public protected default private modifier

Keywords Package Penalty for, Import
Package Penalty for package name: can be separated; the first line must be in the (pre-Import)
. Import package name class name; class name can also use the "*" instead of importing the package represents all the classes
create the system will automatically load a resource package java.lang class, for example, String, system
Example

	package Review.com.aura;

	public class Packaging {
		public  static  void main(String[] args){
	Property pr=new Property("王明",01);
	System.out.println(pr.toString());
	System.out.println(pr.getId());
	System.out.println(pr.getName());

		}
	}
	//封装属性
	class Property{
		private  String name;
		private  int id;

		public Property() {
			super();
		}
		public Property(String name,int id) {
			this.id=id;
			this.name=name;
		}
		public String getName() {
			return name;
		}

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

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		@Override
		public String toString() {
			return "Property{" +
					"name='" + name + '\'' +
					", id=" + id +
					'}';
		}
	}

Polymorphism

Reflect polymorphism:
1, and rewriting method overloading
2, subclass polymorphism image

Syntax
parent class team like name = new subclass (); // parent class reference point to subclass object (subclass can replace any location of the parent class appears)

The object method name ();
method calls the parent class method // compile some time, if the parent class is not being given, if the parent class method in a subclass will also have the rewrite
// child class has no parent class. Method calls subclass instanceof be judged strong turn during the
subclass has no parent in the parent class has put the issue directly invoked
when run on the left to see the right hand side of the equal sign when compiling look

向上转型:子类转父类
向下转型:父类引用子类的对象转子类,需要用instanceof进行判断
对象 instanceof 类型(子类,父类)
注意:       
属性不具备多态
package Review.com.aura;
//多态
public class Polymorphic {
    public  static void  main(String[] args){
        //父类引用指向子类对象
    People st=new Student("小明",10,20);
    //调用父类的特有的方法
    st.demo();
    //调用被子类重写的方法
    st.show();
    //判断是st否是Student类的实例
    if (st instanceof Student ){
        //进行向下转型
    Student su=(Student)st;
    //调用子类特有的方法
    su.test();
            }

        }
}
class People{
    String name;
    int age;
    int id;

    public People() {
        super();
    }
    public People(String name,int age,int id) {
        this.age=age;
        this.id=id;
        this.name=name;

    }
    public  void show(){
        System.out.println("我的名字是"+name+"编号"+id+"年龄"+age);
    }
    public  void demo(){
        System.out.println("这是父类的方法");
    }
}

class Student extends  People{

    String name;
    int age;
    int id;

    public Student() {
        super();
    }
    public Student(String name,int age,int id) {
        this.age=age;
        this.id=id;
        this.name=name;

    }
    public  void show(){
        System.out.println("学生的名字是"+name+"学生编号是"+id+"年龄是"+age);
    }
    public void test(){
            System.out.println("这是一个测试的方法");
    }
}

Guess you like

Origin blog.csdn.net/weixin_44701192/article/details/92403074