课堂代码
public class Demo01 {
public static void main(String[]args){
KeypadDoor keypadDoor = new KeypadDoor();
keypadDoor.open();
keypadDoor.close();
PowerDoor powerDoor = new PowerDoor();
PowerDoor.open();
PowerDoor.close();
Door door = new Door(){
@Override
public void close(){
System.out.println("临时实现的开门");
}
@Override
public void close(){
System.out.println("临时实现关门");
}
};
door.open();
door.close();
}
}
public class Door {
public static void open(){
System.out.println("开门");
}
public static void close(){
System.out.println("关门");
}
}
import edu.xcdq.Demo01.Door;
public class KeypadDoor extends Door {
public static void open(){
System.out.println("请输入密码开门");
}
public static void close(){
System.out.println("轻轻关上,自动锁门");
}
}
import edu.xcdq.Demo01.Door;
public class PowerDoor extends Door {
public static void open(){
System.out.println("遥控开门");
}
public static void close(){
System.out.println("按遥控锁门");
}
}
public abstract class Door {
PowerDoor.open();
PowerDoor.close();
}
public class Door {
public void open(){
System.out.println("开门");
}
public void close(){
System.out.println("关门");
}
}
public class KeypadDoor {
public void open(){
System.out.println("请输入密码开门");
}
public void close(){
System.out.println("轻轻关上,自动锁门");
}
}
public class PowerDoor {
public static void open(){
System.out.println("遥控开门");
}
public static void close(){
System.out.println("按遥控锁门");
}
}
public class Demo01 {
public static void main(String[]args){
Door KeypadDoor = (Door) new KeypadDoor();
KeypadDoor.open();
KeypadDoor.close();
PowerDoor powerDoor = new PowerDoor();
powerDoor.open();
powerDoor.close();
Door.test02();
}
}
//接口,定义门的规范
public interface Door {
//接口的静态常亮
String dasc = "是个门,注意安全";
//public static finalString desc ="是个门,注意安全"
public void open();
public void close();
public default void test01(){
System.out.println("接口中的静态方法");
}
public static void test02(){
System.out.println("接口中的静态方法");
}
}
public class KeypadDoor {
public void open();
public void close();
}
public class PowerDoor {
@Override
public void open() {
System.out.println("键盘开门");
}
@Override
public void close() {
System.out.println("键盘关门");
}
}
抽象类:
1:抽象类关键字 abstract , 修饰类和方法, 可以放在权限访问符的前面或后面 ,一般放在后面
2:抽象类中未必有抽象的方法,但是有抽象方法的一定是抽象类
3:抽象方法的个数没有限制,可以有也可以没有普通的方法
案例:
package edu.xcdq.Demo02.Demo01;
/**
* @version 1.0
* @author: jiazhihao
* @date: 2021-04-29 16:37
*/
public abstract class Door {
public abstract void open();
public abstract void close();
}
子类一:
package edu.xcdq.Demo02.Demo01;
/**
* @version 1.0
* @author: jiazhihao
* @date: 2021-04-29 16:39
*/
public class keypadDoor extends Door {
@Override
public void open(){
System.out.println("输入密码开门");
}
@Override
public void close(){
System.out.println("轻轻关上");
}
}
子类二:
package edu.xcdq.Demo02.Demo01;
public class Main extends Door {
@Override
public void open(){
System.out.println("按遥控开门");
}
@Override
public void close(){
System.out.println("按遥控管么");
}
}
测试:
package edu.xcdq.Demo02.Demo01;
/**
* @version 1.0
* @author: jiazhihao
* @date: 2021-04-29 16:44
*/
public class Demo01 {
public static void main(String[] args) {
keypadDoor keypadDoor = new keypadDoor();
keypadDoor.open();
keypadDoor.close();
Main main = new Main();
main.close();
main.open();
}
}
匿名类:
package edu.xcdq.Demo02.Demo01;
/**
* @version 1.0
* @author: jiazhihao
* @date: 2021-04-29 16:44
*/
public class Demo01 {
public static void main(String[] args) {
keypadDoor keypadDoor = new keypadDoor();
keypadDoor.open();
keypadDoor.close();
Main main = new Main();
main.close();
main.open();
Door door = new Door() {
@Override
public Door open() {
System.out.println("临时实现的开门");
return null;
}
@Override
public void close() {
System.out.println("临时实现的关门");
}
}.open();
}
}
接口
1:关键字 interface ,实现类的关键字 implements
2:接口中定义方法的声明,没有方法的方法体
3:接口中的方法必须是公有的
//接口 ,定义门的规范
public interface Door {
public void open();
public void close();
}
package edu.xcdq.Demo03;
/**
* @version 1.0
* @author: jiazhihao
* @date: 2021-04-29 17:32
*/
public class PowerDoor implements Door {
@Override
public void open() {
}
@Override
public void close() {
}
}
package edu.xcdq.Demo03;
/**
* @version 1.0
* @author: jiazhihao
* @date: 2021-04-29 17:33
*/
public class KeypadDoor implements Door{
@Override
public void open() {
}
@Override
public void close() {
}
}
调用:
package edu.xcdq.Demo03;
/**
* @version 1.0
* @author: jiazhihao
* @date: 2021-04-29 17:43
*/
public class Demo03 {
public static void main(String[] args) {
KeypadDoor keypadDoor =new KeypadDoor();
keypadDoor.close();
keypadDoor.open();
// 可以用接口的类型 接受实现创建的对象
Door powerDoor = new PowerDoor();
powerDoor.open();
powerDoor.close();
}
}
接口中的静态常量
由于接口是规范 实现类必须包含其中的属性 因此 属性默认是公有的 public static finial 原本的属性 就变成了静态的常量
package edu.xcdq.Demo03;
/**
* @version 1.0
* @author: jiazhihao
* @date: 2021-04-29 17:32
*/
//接口 ,定义门的规范
public interface Door {
//接口的静态常量
String desc = "是个门,注意安全";
public void open();
public void close();
}
接口中的个连个特例:
默认方法,只能通过实现类的对象调用 keypadDoor.test01();
静态方法,只能通过接口名的方式调用 Door.test02();
package edu.xcdq.Demo03;
/**
* @version 1.0
* @author: jiazhihao
* @date: 2021-04-29 17:43
*/
public class Demo03 {
public static void main(String[] args) {
KeypadDoor keypadDoor =new KeypadDoor();
keypadDoor.close();
keypadDoor.open();
keypadDoor.test01();
// Door.test01();
// keypadDoor.test02();
Door.test02();
// 可以用接口的类型 接受实现创建的对象
Door powerDoor = new PowerDoor();
powerDoor.open();
powerDoor.close();
}
}