不具体的,似是而非的java
没有具体实现的api
好比Animal,只是对动物的大概描述多线程
能吃ide
能睡this
具体吃啥,怎么睡咱们无从得知,建立出来的对象意义不大spa
咱们认为这种类不该该直接建立对象,应该让其子类建立具体的对象线程
怎么限制?作成抽象类rest
abstract修饰的类能够变成抽象类对象
被abstract修饰的类成为抽象类blog
没法直接建立对象
抽象类中能够存在抽象方法
package com.qf.abs;
public class Demo01 {
public static void main(String[] args) {
// 抽象类没法直接建立对象
// Animal animal = new Animal();
}
}
abstract class Animal{
public abstract void eat();
}
抽象类没法直接建立对象,可是能够有子类
抽象类的子类继承抽象类以后能够获取到抽象类中的非私有普通属性和方法
抽象类的子类若是想使用抽象类中的抽象方法,必须重写这些方法以后才能使用
若是不使用抽象方法,作成抽象类也是能够的
package com.qf.abs;
public class Demo01 {
public static void main(String[] args) {
// 抽象类没法直接建立对象
// Animal animal = new Animal();
Husky husky = new Husky();
System.out.println(husky.type);
husky.breath();
husky.eat();
}
}
/**
* 抽象类Animal
* 描述动物的一个类
* 只作了大概的描述,没有具体的实现
* 须要子类继承此类后作具体的描述
* @author Dushine2008
*
*/
abstract class Animal{
// 属性
String type = "动物";
String section;
public abstract void eat();
public abstract void sleep();
public void breath() {
System.out.println("全部的动物都依赖氧气存活...");
}
}
abstract class Dog extends Animal{
}
class Husky extends Dog{
建立抽象类Car
建立Car的子类
Auto
Bus
Tractor
在子类重写继承的抽象方法和本身独有的方法
使用多态的思想建立对象并调用这些方法
package com.qf.abs;
public class Demo02 {
public static void main(String[] args) {
Car car01 = new Auto();
car01.start();
car01.stop();
// 向下转型
Auto auto = (Auto) car01;
auto.manned();
System.out.println("========================");
Car car02 = new Bus();
car02.start();
car02.stop();
// 拆箱
Bus bus = (Bus) car02;
bus.manned();
System.out.println("========================");
Car car03 = new Tractor();
car03.start();
car03.stop();
Tractor tractor = (Tractor) car03;
tractor.doFarmWork();
}
}
/**
* 车的顶层类
* 抽象类
* 方法没有具体的实现
* @author Dushine2008
*
*/
abstract class Car{
// 属性
int weight;
int height;
String brand;
int price;
// 方法
public abstract void start();
public abstract void stop();
}
/**
* 奥拓车
* 继承Car
* 重写启动和中止的方法
* @author Dushine2008
*
*/
class Auto extends Car{