封装,继承,多态
封装:
package com.oop; import com.oop.demo03.Pet; import com.oop.demo04.Student; /* 1.提高程序的安全性,保护数据 2.隐藏代码的实现细节 3.统一接口 4.系统的可维护性提高了 */ //一个项目应该只存在一个main方法 public class Application { public static void main(String[] args) { Student s1 = new Student(); s1.setName("貓"); System.out.println(s1.getName()); s1.setAge(130); System.out.println(s1.getAge()); } } /* package com.oop.demo04; //类 private:私有 public class Student { //属性私有 //名字 private String name; //学号 private int id; //性别 private char sex; private int age; //学习() //睡觉() //提供一些可以操作这个属性的方法 //提高一些public的get,set方法 //get获得这个数据 public String getName(){ return this.name; } //set给这个数据设置值 public void setName(String name){ this.name=name; } //alt+insert 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<120&&age>0){ this.age = age; }else { this.age=3; } } } */
继承:
package com.oop; import com.oop.demo05.Student; //一个项目应该只存在一个main方法 public class Application { public static void main(String[] args) { Student student = new Student(); student.say(); } } /* package com.oop.demo05; public class Person { //在java中所有类都默认继承object类 //public 能给派生类 //private 私有不能给派生类,可以通过get/set来给派生类 public void say(){ System.out.println("说了一句话"); } } */ /* package com.oop.demo05; //子类继承父类的全部方法 public class Student extends Person{ //ctrl+h 打开继承关系 } */
super详解:
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super 必须 只能出现在子类的方法或者构造方法中
3.super 和this不能同时调用 和构造方法
vs this:
代表的对象不同
this:本身调用者这个对象
super:代表父类对象的应用
前提:
this;没有继承也可以使用
super :只能在继承条件下才能使用
构造:
this():本类的构造
super():父类构造
package com.oop; import com.oop.demo05.Student; //一个项目应该只存在一个main方法 public class Application { public static void main(String[] args) { Student student = new Student(); // student.test("maomia"); // student.test1(); } } /* package com.oop.demo05; public class Person { //在java中所有类都默认继承object类 //public 能给派生类 //private 私有不能给派生类,可以通过get/set来给派生类 protected String name="mao"; public Person() { System.out.println("Person无参构造器"); } //super 无法调用私有 public void print(){ System.out.println("Person"); } } */ /* package com.oop.demo05; //子类继承父类的全部方法 public class Student extends Person{ //ctrl+h 打开继承关系 private String name="maomi"; public Student() { //隐藏代码:调用了父类的无参构造 super(); //调用父类的构造器必须放在子类的第一行 System.out.println("Student无参构造器"); } public void print(){ System.out.println("Student"); } public void test1(){ print();//Student this.print();//Student super.print();//Person } public void test(String name){ System.out.println(name);//maomia System.out.println(this.name);//maomi 此类 System.out.println(super.name);//mao 父类 } } */
方法的重写:
重写:
需要用继承 子类重写父类的方法
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大:public>Proctected>Default>private
4.抛出异常:范围,可以被缩小,但不能扩大 classnotfoundException---->Exception
重写,子类的方法必须和父类一致 方法体不同
为什么需要重写?
1.父类的功能:子类不一定需要,或者不一定满足
alt+inset ---------》override
package com.oop; import com.oop.demo05.A; import com.oop.demo05.B; import com.oop.demo05.Student; //一个项目应该只存在一个main方法 //静态方法:方法调用只和左边,定义的数据类型有关 //非静态方法:重写 只能public public class Application { //静态的方法和非静态的方法的区别很大 public static void main(String[] args) { //方法的调用 A a = new A(); a.test(); //父类的引用指向了子类 B b = new A();//子类重写了父类的方法 b.test(); } } /* package com.oop.demo05; public class A extends B{ //alt+insert @Override//注解:有功能的注释 单词意思:重写 public void test() { System.out.println("A->test"); } } */ /* package com.oop.demo05; //重写都是方法的重写,和属性无关 public class B { public void test(){ System.out.println("B->test"); } } */
多态:
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常 ClassCastExportion
3.存在条件:继承关系,方法需要重写 父类引用指向子类 father f1=new Son()
不能重写:
1.static 方法,属于类,它不属于实例
2.final 常量
3.private 方法
package com.oop; import com.oop.demo06.Person; import com.oop.demo06.Student; public class Application { public static void main(String[] args) { //一个对象的实际类型是确定的 // new Student(); //可以指向的引用类型就不确定啦:父类的引用指向子类 //Student能调用的方法都是自己的或者是继承父类的 Student s1 = new Student(); //person父类可以指向子类,不能调用子类的方法 Person s2 = new Student(); Object s3 = new Student(); s2.run();//子类重写了父类的方法 ,执行子类的方法 s1.run(); //对象能执行那些方法,主要看对象左边的类型和右边关系不大 //s2.eat(); 无法用 s1.eat(); } } /* package com.oop.demo06; public class Person { public void run(){ System.out.println("run"); } } */ /* public class Student extends Person{ @Override public void run() { System.out.println("son"); } public void eat(){ System.out.println("eat"); } } */
instanceof:
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
public static void main(String[] args) {
//Object>String
//Object>Person>Teacher
//Object>Person>student
Object object = new Student();
//System.out.println(X instanceof Y); 能不能编译通过 就看有没有父子关系
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
System.out.println("====================");
Person person = new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//false
//System.out.println(person instanceof String);//编译报错
System.out.println("====================");
Student student = new Student();
System.out.println(student instanceof Student);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
// System.out.println(student instanceof Teacher);//编译报错
// System.out.println(student instanceof String);//编译报错
}
}
类型转换:
package com.oop; import com.oop.demo06.Person; import com.oop.demo06.Student; import com.oop.demo06.Teacher; public class Application { public static void main(String[] args) { //类型之间的转化 父类 子类 //高 低 Person obJ = new Student(); //将obj这个对象转换为Student类型,我们就可以使用Student类型的方法 Student student = (Student) obJ; student.go(); //子类转化为父类 可能丢失一下方法 Student student1=new Student(); student1.go(); Person person=student1; } } /* 1.父类的引用指向子类的对象 2.把子类转换为父类,向上转换 3.把父类转换为子类,向下转换 强制转换 可能丢失方法 4.方便方法的调用,减少重复的代码 抽象:封装,继承,多态 */