封装
封装从字面上来理解就是包装的意思
对象封装的是自己的属性和方法
1、良好的封装能够减少耦合。
2、类内部的结构可以自由修改。
3、可以对成员进行更精确的控制。
4、隐藏信息,实现细节
继承
继承是使用已存在的类的定义作为基础建立新类的技术,新类的定义可以增加新的数据或新的功能,也可以用父类的功能,但不能选择性地继承父类。is-------a的关系
1、子类拥有父类非private的属性和方法。
2、子类可以拥有自己属性和方法,即子类可以对父类进行扩展。
3、子类可以用自己的方式实现父类的方法。(以后介绍)
继承存在如下缺陷:
1、父类变,子类就必须变。
2、继承破坏了封装,对于父类而言,它的实现细节对与子类来说都是透明的。
3、继承是一种强耦合关系。
多态
即一个引用变量倒底会指向哪个类的实例对象,该引用变量发出的方法调用到底是哪个类中实现的方法,必须在由程序运行期间才能决定。指向子类的父类引用由于向上转型了,它只能访问父类中拥有的方法和属性,而对于子类中存在而父类中不存在的方法,该引用是不能使用的,尽管是重载该方法。若子类重写了父类中的某些方法,在调用该些方法的时候,必定是使用子类中定义的这些方法(动态连接、动态调用)。
多态实现的条件
多态有三个必要条件:继承、重写、向上转型。
继承:在多态中必须存在有继承关系的子类和父类。
重写:子类对父类中某些方法进行重新定义,在调用这些方法时就会调用子类的方法。
向上转型:在多态中需要将子类的引用赋给父类对象,只有这样该引用才能够具备技能调用父类的方法和子类的方法。
继承案例
编写上述类
运行结果
package comExtends.People; import comExtends.Object.Object; public class People extends Object{ public int age ; //年龄 public String sex;//性别 public int height ;//身高 public int weight; //体重 public String name; public People() { super(); // TODO Auto-generated constructor stub } public People(int age, String sex, int height, int weight, String name) { super(); this.age = age; this.sex = sex; this.height = height; this.weight = weight; this.name = name; } /** * @return the age */ /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the sex */ public String getSex() { return sex; } /** * @param sex the sex to set */ public void setSex(String sex) { this.sex = sex; } /** * @return the height */ /** * @param height the height to set */ public void setHeight(int height) { this.height = height; } /** * @return the weight */ /** * @param weight the weight to set */ public void setWeight(int weight) { this.weight = weight; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /* * 动作 */ public void peopleeat() { System.out.println("人类吃饭要积极"); } public void peoplesleep() { System.out.println("睡觉要开心,做个美梦哦"); } public void beat() { System.out.println("打嘟嘟 xing xing hubin "); } public void learn() { System.out.println("no pains no gains 学习要积极"); } }
package comExtends.People; import comExtends.Object.Object; public class doctor extends Object { public doctor() { super(); // TODO Auto-generated constructor stub } public doctor(int age, String sex, int height, int weight, String color, String name, String autoplast) { super(); // TODO Auto-generated constructor stub } @Override public void eat() { // TODO Auto-generated method stub super.eat(); } @Override public void sleep() { // TODO Auto-generated method stub System.out.println("医生在睡觉"); } @Override public void run() { // TODO Auto-generated method stub System.out.println("医生在跑步"); } @Override public void swwing() { // TODO Auto-generated method stub System.out.println("医生在游泳"); } @Override public void beat() { // TODO Auto-generated method stub System.out.println("医生在打豆豆"); } public void savepeople() { System.out.println("医生savepeople"); } }
package comExtends.People; /** * 军人 * @author MZFAITHDREAM * */ public class Solder extends People{ public int age ; //年龄 public String sex;//性别 public int height ;//身高 public int weight; //体重 public String name; public Solder() { super(); // TODO Auto-generated constructor stub } public Solder(int age, String sex, int height, int weight, String name) { super(age, sex, height, weight, name); // TODO Auto-generated constructor stub } public void tranning() { System.out.println("tranning 顺利了"); } public void run() { System.out.println("run run go go"); } public void playgrams() { System.out.println("play play play "); } }
package comExtends.People; public class sportsman extends People{ public sportsman() { // TODO Auto-generated constructor stub System.out.println("运动员"); } public sportsman(int age, String sex, int height, int weight, String name) { super(age, sex, height, weight, name); // TODO Auto-generated constructor stub } @Override public void setAge(int age) { // TODO Auto-generated method stub super.setAge(age); } @Override public String getSex() { // TODO Auto-generated method stub return super.getSex(); } @Override public void setSex(String sex) { // TODO Auto-generated method stub super.setSex(sex); } @Override public void setHeight(int height) { // TODO Auto-generated method stub super.setHeight(height); } @Override public void setWeight(int weight) { // TODO Auto-generated method stub super.setWeight(weight); } @Override public String getName() { // TODO Auto-generated method stub return super.getName(); } @Override public void setName(String name) { // TODO Auto-generated method stub super.setName(name); } @Override public void peopleeat() { // TODO Auto-generated method stub super.peopleeat(); } @Override public void peoplesleep() { // TODO Auto-generated method stub super.peoplesleep(); } @Override public void beat() { // TODO Auto-generated method stub super.beat(); } @Override public void learn() { // TODO Auto-generated method stub System.out.println("sportsman在学习"); super.learn(); } }
package comExtends.People; public class Student extends People{ public int age ; //年龄 public String sex;//性别 public int height ;//身高 public int weight; //体重 public String name; public Student() { super(); // TODO Auto-generated constructor stub } public Student(int age, String sex, int height, int weight, String name) { super(age, sex, height, weight, name); // TODO Auto-generated constructor stub } public void dohomework() { System.out.println("学生写字"); } }
package comExtends.People; public class Teacher extends People{ public int age ; //年龄 public String sex;//性别 public int height ;//身高 public int weight; //体重 public String name; public Teacher() { super(); } public Teacher(int age, String sex, int height, int weight, String name) { super(age, sex, height, weight, name); // TODO Auto-generated constructor stub } public void learnenglish() { System.out.println("can not obery himself is to other"); } public void learnmath() { System.out.println("math 学习数学强思路"); } public void learnchinese() { System.out.println("学习语文提高文化素质"); } }
package comExtends.People; public class PeopleTest { public static void main(String[] args) { //1 People p =new People(); System.out.println("继承用extends修饰,指的是x属于y的一种"); System.out.println("继承是is...........a的关系"); System.out.println("student and teacher or solder是 people的一种"); System.out.println(p); p.name ="小王"; p.height =148; p.weight =67; p.age=89; p.sex="nv"; System.out.println(p.name); System.out.println(p.height+"cm"); System.out.println(p.weight+"kg"); System.out.println(p.age+"nain"); System.out.println(p.sex); p.beat(); p.peoplesleep(); p.peopleeat(); p.learn(); System.out.println("==================对象一==========================="); //2 Student s =new Student(); System.out.println(s); s.age=20; s.name="限胡"; s.height =178; s.weight =67; s.age=89; s.sex="nan"; System.out.println(s.name); System.out.println(s.height+"cm"); System.out.println(s.weight+"kg"); System.out.println(s.age+"nain"); System.out.println(s.sex); s.peopleeat(); s.beat(); s.learn(); s.dohomework(); System.out.println("==================对象二==============================="); //3 Teacher t =new Teacher(); System.out.println(t); t.name="户外"; t.age=34; t.height=167; t.weight=64; t.sex="nan"; //成员变量 System.out.println(t.name); System.out.println(t.height+"cm"); System.out.println(t.weight+"kg"); System.out.println(t.age+"nain"); System.out.println(t.sex); //成员方法 t.learnchinese(); t.learnenglish(); t.learnmath(); t.beat(); t.learn(); System.out.println("==================对象三=================="); Solder d =new Solder (); d.name="户外yu"; d.age=34; d.height=167; d.weight=64; d.sex="nan"; System.out.println(d.name); System.out.println(d.height+"cm"); System.out.println(d.weight+"kg"); System.out.println(d.age+"nain"); System.out.println(d.sex); //成员方法 d.playgrams(); d.learn(); d.run(); d.tranning(); d.beat(); d.learn(); System.out.println("-----------------对象四"); People e =new sportsman (); e.beat(); e.learn(); } }
抽象类不能被实例化(初学者很容易犯的错),如果被实例化,就会报错,编译无法通过。只有抽象类的非抽象子类可以创建对象。
抽象类中不一定包含抽象方法,但是有抽象方法的类必定是抽象类。
抽象类中的抽象方法只是声明,不包含方法体,就是不给出方法的具体实现也就是方法的具体功能。
构造方法,类方法(用 static 修饰的方法)不能声明为抽象方法。
抽象类的子类必须给出抽象类中的抽象方法的具体实现,除非该子类也是抽象类。