案例一:
Vehicle:
package Abstract;
public abstract class Vehicle {
//public abstract void numOfWheel();
public abstract String numOfWheel();
}
Car:
package Abstract;
public class Car extends Vehicle {
@Override
public String numOfWheel() {
return "四轮车";
}
/*@Override
public void numOfWheel() {
System.out.println("四轮车");
}*/
}
Motorbike:
package Abstract;
public class Motorbike extends Vehicle {
@Override
public String numOfWheel() {
return "双轮车";
}
/*@Override
public void numOfWheel() {
System.out.println("双轮车");
}*/
}
Test:
package Abstract;
import org.junit.Test;
public class jTest {
@Test
public void test(){
Car car=new Car();
Motorbike mk=new Motorbike();
/*car.numOfWheel();
mk.numOfWheel();*/
String name1=car.numOfWheel();
System.out.println(name1);
String name2=mk.numOfWheel();
System.out.println(name2);
}
}
案例二:
Motovercal:
package Abstract2;
public abstract class Motovercal {
/**
* 抽象类
* 1.如果在程序设计过程中,定义了某个方法不需要该类来实现,
* 需要子类来重写,那么该方法应该定义为抽象方法,通过abstract关键字进行修饰
* 2.如果某各类中定义了抽象方法,那么该类务必定义为抽象类。
* 3.抽象方法只需要定义,不需要实现,谁继承,谁实现
*/
private String id;
private String type;
private double price;
public Motovercal(String id, String type, double price) {
this.id = id;
this.type = type;
this.price = price;
}
public abstract void show();//定义抽象方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Car:
package Abstract2;
public class Car extends Motovercal {
private String color;
public Car(String id, String type, double price, String color) {
super(id, type, price);
this.color = color;
}
@Override
public void show() {
System.out.println("本车车牌号为:"+super.getId()+"类型为:"+super.getType()+"价格为:"+super.getPrice()+"座位:"+this.color);
}
}
Bus :
package Abstract2;
public class Bus extends Motovercal {
private int seatCount;
public Bus(String id, String type, double price,int seatCount) {
super(id, type, price);
this.seatCount=seatCount;
}
@Override
public void show() {
System.out.println("本车车牌号为:"+super.getId()+"类型为:"+super.getType()+"价格为:"+super.getPrice()+"座位:"+this.seatCount);
}
}
Test:
package Abstract2;
import org.junit.Test;
public class jTest {
@Test
public void test(){
Motovercal mk1=new Car("A00001", "jeep",450000,"yellow");
Motovercal mk2=new Bus("B00002", "金龙", 350000, 30);
mk1.show();
mk2.show();
}
}
案例三:
Pet:
package Pet;
public abstract class Pet {
private String name;
private String health;
private String love;
public Pet(String name, String health, String love) {
this.name = name;
this.health = health;
this.love = love;
}
public abstract void eat();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHealth() {
return health;
}
public void setHealth(String health) {
this.health = health;
}
public String getLove() {
return love;
}
public void setLove(String love) {
this.love = love;
}
}
Penguin:
package Pet;
public class Penguin extends Pet {
public String sex;
public Penguin(String name, String health, String love, String sex) {
super(name, health, love);
this.sex = sex;
}
@Override
public void eat() {
System.out.println("吃鱼!");
}
}
Dog:
package Pet;
public class Dog extends Pet {
public String strain;
public Dog(String name, String health, String love, String strain) {
super(name, health, love);
this.strain = strain;
}
@Override
public void eat() {
System.out.println("啃骨头!");
}
}
PatFactory:
package Pet;
public class PatFactory {
public void getPet(String pet){
if("小花".equals(pet)){
System.out.print("小花的小狗");
}else if("QQ".equals(pet)){
System.out.print("QQ的企鹅");
}else{
System.out.println("输入错误");
}
}
}
Test:
package Pet;
import org.junit.Test;
public class Jtest {
@Test
public void test(){
Dog dog=new Dog("小花", "健康值为100,", "亲密度为20,", "拉布拉多犬");
PatFactory pf=new PatFactory();
System.out.print("名叫:");
pf.getPet("小花");
System.out.print("是一只"+dog.strain);
System.out.print(dog.getHealth()+dog.getLove()+"跑回来");
dog.eat();
Penguin pg=new Penguin("小花", "健康值为100,", "亲密度为20,", "拉布拉多犬");
//PatFactory p=new PatFactory();
System.out.print("名叫:");
pf.getPet("QQ");
System.out.print("是个"+pg.sex);
System.out.print(dog.getHealth()+dog.getLove()+"跑回来");
dog.eat();
}
}