java--面向对象之抽象类

案例一:


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();
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值