
抽象类体现的是模板设计的思想,其中可以有抽象方法,也可桥好以有普通方法,但是抽象类不能
new,其抽象方法需要其子类来实现。
下面是我写的一个例子:
*/
public class Test10 extends 弯消中Person
{
public static void main(String[] args) {
埋山 new Test10("司机小王").say()
}
public Test10(String name) {
super(name)
}
@Override
String work() {
return "开车"
}
}
abstract class Person
{
String name
public Person(String name){
this.name=name
}
void say(){
System.out.println(name+work())
}
abstract String work()
}
package test/**
*
* @author JinnL
*父类抽象类
*/
public abstract class Car {
//转弯
abstract void turn()
//启动
abstract void start()
void what(){
System.out.println("this is "+this.getClass().getSimpleName())
}
public static void main(String[] args) {
/**
* 方法入口
*/
Car[] cars ={new Bicycle(),new Automobile(),new GasAutomobile(),new DieselAutomobile()}
for (Car car : cars) {
car.start()
}
}
}
class Bicycle extends Car{
@Override
void turn() {
System.out.println("this is "+this.getClass().getSimpleName())
}
@Override
void start() {
System.out.println("this is "+this.getClass().getSimpleName())
}
void what(){
}
}
class Automobile extends Car{
@Override
void turn() {
System.out.println("this is "+this.getClass().getSimpleName())
}
@Override
void start() {
System.out.println("this is "+this.getClass().getSimpleName())
}
}
class GasAutomobile extends Automobile{
//重写start turn
@Override
void turn() {
System.out.println("this is "+this.getClass().getSimpleName())
}
@Override
void start() {
System.out.println("this is "+this.getClass().getSimpleName())
}
}
class DieselAutomobile extends Automobile{
@Override
void start() {
System.out.println("this is "+this.getClass().getSimpleName())
}
void what(){
System.out.println("this is "+this.getClass().getSimpleName())
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)