package com.mingmao.javaobjectoriented.programming; /* 封装的意义: 提高安全性 隐藏代码实现细节 统一接口,形成规范 提高系统的可维护性 */ //学生类 public class StudentEncapsulation { //封装主要是封装属性,方法封装用的很少,private //属性私有,只有本类中可使用 private String name; //姓名 private int id; //学号 private char sex; //性别 private int age; //年龄 //获取姓名 public String getName(){ return this.name; } //设置姓名 public void setName(String name){ this.name=name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { if(age>130 || age<0){ System.out.println("您输入的年龄有误,请重新输入"); }else{ this.age = age; } } }
package com.mingmao.javaobjectoriented.programming; public class Application { public static void main(String[] args) { StudentEncapsulation student=new StudentEncapsulation(); student.setName("mingmao"); System.out.println(student.getName()); student.setAge(200); } }
查看继承树:Navigat-Type Hierarchy
package com.mingmao.javaobjectoriented.programming.inherit; /* 修饰符: public:跨包,跨类 protected:本类,本包,不同包的子类 default:默认,可以不写,同一个包内可访问 private:只有本类可访问 */ //在Java中,都直接或间接默认继承Object类 //Java中只有单继承,没有多继承 //父类 public class Person { //public int money=10_0000_0000; private int money=10_0000_0000; public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } public void say(){ System.out.println("说了一句话"); } }
package com.mingmao.javaobjectoriented.programming.inherit; //继承,子类 //子类继承了父类,会拥有父类所有方法 public class Student extends Person { }
package com.mingmao.javaobjectoriented.programming.inherit; //继承,子类 public class Teacher extends Person{ }
package com.mingmao.javaobjectoriented.programming; import com.mingmao.javaobjectoriented.programming.inherit.Student; public class Application { public static void main(String[] args) { Student student=new Student(); System.out.println(student.getMoney()); student.say(); } }
package com.mingmao.javaobjectoriented.programming.inherit1; public class Person { protected String name="mingmao"; public Person() { System.out.println("person无参构造器执行了"); } protected void print(){ System.out.println("person"); } }
package com.mingmao.javaobjectoriented.programming.inherit1; public class Student extends Person { private String name="mingmao1"; public Student() { //隐藏代码,默认调用了父类的无参构造 // super(); //调用父类的构造方法 System.out.println("student无参构造器执行了"); } public void printMethod(){ print(); //调用本类的print方法 this.print();//调用本类的print方法 super.print();//调用父类的print方法 } public void printName(String name){ System.out.println(name);//传参 System.out.println(this.name);//本类属性 System.out.println(super.name);//父类属性 } protected void print(){ System.out.println("student"); } }
package com.mingmao.javaobjectoriented.programming; import com.mingmao.javaobjectoriented.programming.inherit1.Student; public class Application { public static void main(String[] args) { Student student=new Student(); /* person无参构造器执行了 student无参构造器执行了 */ student.printName("mingmao2"); /* mingmao2 mingmao1 mingmao */ student.printMethod(); /* student student person */ } }
package com.mingmao.javaobjectoriented.programming.inherit2; public class B { public static void test(){ System.out.println("B=>test"); } }
package com.mingmao.javaobjectoriented.programming.inherit2; //重写都是方法的重写,和属性无关 public class A extends B { public static void test(){ System.out.println("A=>test"); } }
package com.mingmao.javaobjectoriented.programming; import com.mingmao.javaobjectoriented.programming.inherit2.A; import com.mingmao.javaobjectoriented.programming.inherit2.B; public class Application { public static void main(String[] args) { A a=new A(); a.test();//A=>test B b=new B(); b.test();//B=>test //父类的引用指向了子类 B b1=new A(); b1.test();//B=>test //方法的调用只和左边的类型有关 } }
上述基础上去掉static
方法重写快捷生成:右键Generate-Override-Methods...
package com.mingmao.javaobjectoriented.programming.inherit3; public class B { public void test(){ System.out.println("B=>test"); } }
package com.mingmao.javaobjectoriented.programming.inherit3; public class A extends B { @Override //此为自动生成的注解,有功能 public void test() { System.out.println("A=>test"); } }
package com.mingmao.javaobjectoriented.programming; import com.mingmao.javaobjectoriented.programming.inherit3.A; import com.mingmao.javaobjectoriented.programming.inherit3.B; public class Application { public static void main(String[] args) { A a=new A(); a.test();//A=>test B b=new B(); b.test();//B=>test B b1=new A(); b1.test();//A=>test /* 静态方法与非静态方法有很大区别: 静态方法:与类一起加载,方法的调用只与左边类型有关 非静态方法:可以重写,方法的调用与右边new的对象有关 */ } }
父类引用指向子类
父类只能调用父类中有的方法
如果父类中的方法被子类重写则执行子类的方法
package com.mingmao.javaobjectoriented.programming.polymorphic; public class Person { public void run(){ System.out.println("run=>person"); } }
package com.mingmao.javaobjectoriented.programming.polymorphic; public class Student extends Person{ @Override public void run() { System.out.println("run=>student"); } public void eat(){ System.out.println("eat=>student"); } }
package com.mingmao.javaobjectoriented.programming; import com.mingmao.javaobjectoriented.programming.polymorphic.Person; import com.mingmao.javaobjectoriented.programming.polymorphic.Student; public class Application { public static void main(String[] args) { //一个对象的实际类型是确定的 //new Student; //new Person; //但是可以指向的引用类型就不确定了 //父类的引用指向子类 Student s1=new Student(); Person s2=new Student(); Object s3=new Student(); s1.run(); s2.run(); s1.eat(); //s2.eat();出错 //总结 Person p=new Student(); //父类引用指向子类 //父类只能调用父类中有的方法 //如果父类中的方法被子类重写则执行子类的方法 p.run(); } }
学习视频