封装
-
"高内聚,低耦合"
-
属性私有,get/set
extends 的意思是 “扩展” 。子类是父类的扩展。
重写//重写都是方法的重写,和属性无关
public class B {
public void test(){
System.out.println("B->test()");
}
}
public class A extends B{
//Alt + Insert
@Override //重写 注解:有功能的注释!
public void test() {
System.out.println("A->test()");
}
}
//静态方法和非静态的方法区别很大!
// 静态:方法的调用只和左边,定义的数据类型有关
// 非静态:重写
public static void main(String[] args) {
A a = new A();
a.test(); //A
//父类的引用指向子类
B b = new A(); // 若子类重写了父类的方法 执行子类方法
b.test(); //A
多态
多态注意事项: 1. 多态是方法的多态,属性没有多态 2. 父类和子类,有联系 类型转换异常ClassCastException! 3. 存在条件: 继承关系,方法需要重写,父类引用指向子类对象!Father f1 = new Son(); 不能重写的情况: 1. static 方法, 属于类,不属于实例 2. final 常量; 3. private 方法;
public class Person {
public void run(){
System.out.println("dad");
}
}
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat() {
System.out.println("eat");
}
}
//一个对象的实际类型是确定的 //可以指向的引用类型就不确定了: 父类的引用可以指向子类 //new Student(); //new Person(); //子类能调用的方法都是自己的 或者继承父类的! Student s1 = new Student(); //父类 可以指向子类,但是不能调用子类独有的方法 Person s2 = new Student(); Object s3 = new Student(); //对象能执行哪些方法,主要砍对象左边的类型,和右边关系不大! s2.run();// 若子类重写了父类的方法 执行子类方法 s1.run(); s1.eat(); //s2.eat(); ((Student) s2).eat(); //强制转换instanceof
//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); //编译报错
抽象类
//abstract 抽象类:本质是类 需要被继承extends 单继承 (接口可以多继承)
public abstract class Action {
//约束 有人帮我们实现
// 抽象方法,只有方法名字,没有方法的实现!
public abstract void doSomething();
//抽象类特点:
//1. 不能new这个抽象类,只能靠子类去实现它:约束!
//2. 抽象类中可以写普通方法
//3. 抽象方法必须在抽象类中
//抽象的抽象:约束
//思考题?
//1. 抽象类不能new对象,存在构造器吗?
//2. 存在的意义是什么 提高开发效率,可扩展性
}
//抽象类的所有方法,继承了它的子类都必须要实现它的方法 除非它的子类也是抽象类,就由子子类实现...
public class A extends Action{
//Alt + Insert
@Override
public void doSomething() {
}
public static void main(String[] args) {
System.out.println("zzzz");
}
}
接口
//抽象的思维~ Java 架构师~ //interface 定义的关键字 , 接口都需要有实现类 //接口中的所有定义的属性都是常量 默认: public static final //接口中的所有定义的方法其实都是抽象的 默认:public abstract // 抽象类: extends // 类 可以实现接口 ----> implements 接口 // 实现了接口的类,就需要重写接口中的方法 Alt + Insert // 多继承 利用接口实现 implements 接口名,接口名,接口名...
接口的作用: 1. 约束 2. 定义一些方法,让不同的人实现 3. 定义的方法 默认:public abstract 4. 定义的常量 默认:public static final 5. 接口不能被实例化,接口中没有构造方法 6. implements可以实现多个接口 7. 实现接口必须要重写接口中的方法 8. 总结博客异常
快捷键:选中代码 ---> Ctrl + Alt + T



