深入理解java抽象与接口

抽象类

抽象关键词abstract

1,abstract 修饰类  抽象类

2,abstract 修饰方法 抽象方法

3,抽象方法只有声明,而没有具体的方法实现部分

注意:抽象类中的抽象方法,强制其子类重写抽象方法

抽象类不能创建对象,因为其含有无具体实现的方法

抽象类创建对象的方式可仿照多态

抽象类中不一定有抽象方法,但抽象方法一定在抽象类中

其抽象方法无具体实现,所以没法自身创建对象,且会强制子类重写其抽象类

此外和普通类没区别

抽象类中可以没抽象方法吗?作用是什么

可以没抽象方法,作用是禁止此类创建对象,而只能使用其子类

abstract 与static , final ,private 不能共存

static 修饰的静态方法需用类名调用

而abstract修饰的抽象方法没方法的实现,只有声明,无法调用,所以不能喝static 一起使用

final修饰方法后不可重写

而abstract修饰方法会强制子类重写抽象方法,所以不能共存

private 修饰的方法只能本类访问,而子类访问不到,无法重写

接口

接口是对行为的抽象

使用interface 修饰   //interface A{   }

接口特点:

1)接口只能声明抽象方法

2)如果不写public abstract 修饰接口声明方法,系统会默认添加

3)接口无法创建对象,要使用接口,就要去实现接口中的方法

4)接口的实现 使用implements

接口注意

只能有常量public static final 常量名 = 初值;和抽象方法

当你在接口声明常量系统会默认public static final

类和类的关系:单或多层继承

类和接口的关系:实现关系,可以有单实现和多实现

class A implements C, S{}

接口和口的关系:可以单继承,也可多继承

interface Fly2 extends Fly, Fly1{
	public abstract void flyy();
}


* 动物类 姓名 年龄 吃饭 睡觉
 * 猫和超级猫类
 * 超级猫接口:飞


/*
 * * 动物类 姓名 年龄 吃饭 睡觉
 * 猫和超级猫类
 * 超级猫接口:飞
 */
public class Demo04 {

}
abstract class Animal{
	private String name;
	private int age;
	public Animal() {
		
	}
	public Animal(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	public abstract void eat();
	public abstract void sleep();
		
}
class Cat extends Animal{
	public Cat() {
		super();
	}
	public Cat(String name ,int age) {
		super(name,age);
	}
	@Override
	public void eat() {
	}
	@Override
	public void sleep() {
	}
}
interface Fly{
	public abstract void fly();
}
interface Fly1{
	public abstract void fly1();
}
interface Fly2 extends Fly, Fly1{
	public abstract void flyy();
}

class SuperCat extends Animal implements Fly2{
	public SuperCat() {
		super();
	}
	public SuperCat(String name ,int age) {
		super(name,age);
	}
	
	@Override
	public void eat() {
	}
	@Override
	public void sleep() {	
	}
	@Override
	public void fly() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void flyy() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void fly1() {
		// TODO Auto-generated method stub
		
	}	
	}

* 汽车租赁: 某公司分为小汽车和客车两种车型, 小汽车有宝马,本田,奇瑞等型号, 客车分为18座位和23座位的,
计算租金(小汽车100一天,18座200一天,23座250一天)  提示:  抽象车类(Vehicle), 子类分别是小汽车(Bus)类 和 客车(Car)类
 */
public class Demo06 {
	

}
abstract class Vehicle{
	int seat = 1;
	
	public abstract void monny();
	
}
interface Seat{
	public abstract void seat();
}
interface Model{
	public abstract void model();	
}

class Bus extends Vehicle implements Seat{
	
	public void monny() {
		System.out.println("每天计算租金");
	}
	public void seat() {
		System.out.println("18座200和13座250");
	}
}	
class Car extends Vehicle implements Model{
	public void monny() {
		System.out.println("每天计算租金");
	}
	public void model() {
		System.out.println("宝马,本田,奇瑞100一天");
	}
}
/*
 * 创建一个打印机类Printer, 定义抽象方法print(), 创建两个子类,针式打印机(DotMatriixPrinter) 
 * 和喷墨打印机(InkperPrinter), 在各自的类中重写方法print(),编写测试类实现两种打印机,
 * 再添加一个激光打印机子类(LaserPrinter), 重写方法print(), 修改测试类实现该打印机打印
 */
public class Demo05 {
	public static void main(String[] args) {
		DotMatriixPrinter dot = new DotMatriixPrinter();
		InkperPrinter ink = new InkperPrinter();
		LaserPrinter laser = new LaserPrinter();
		dot.print();
		ink.print();
		laser.print();
	}
}
abstract class Printer{
	public abstract void print();
}
class DotMatriixPrinter extends Printer{
	public void print() {
		System.out.println("针式打印机");
	}
}
class InkperPrinter extends Printer{
	public void print() {
		System.out.println("喷墨打印机");
	}
}
class  LaserPrinter extends Printer{
	public void print() {
		System.out.println("激光打印机");
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值