复习了一下java基础,随手做个例子,就当作笔记用吧!
[color=red]抽象类:[/color]
/**
* 抽象类一般声明这个类所拥有的基本特征(属性)和行为(方法)
* 里面可以有非抽象的方法
*
*/
public abstract class AbstractCar {
/**
* 抽象类中可以有抽象方法,也可以有其他方法
*/
public int lunzi = 2;// 轮子
// 抽象方法不需要{},由子类中具体实现
public abstract void move(); // 移动方法
// 不是抽象方法就需要{}
public void yinqing() {
System.out.println("抽象类中引擎类型方法:");
};
}
[color=red]以下是抽象类的两个子类:[/color]
public class Car extends AbstractCar {
/**
* 如果子类中没有定义与父类相同的属性,this.xx调用的父类中的属性 此时 给this.xx或super.xx的属性赋值都是给父类属性重新赋值
*
*/
private int lunzi;
/**
*
* move方法是父类抽象方法的具体实现
*/
@Override
public void move() {
this.lunzi = 4;// 本类如果没声明lunzi属性就会重新给父类lunzi赋值
System.out.println(this.lunzi);
System.out.println("父类默认属性:" + super.lunzi + "只轮子");
// TODO Auto-generated method stub
System.out.println("我是汽车,要跑了!");
super.yinqing();
this.yinqing();
}
public void yinqing() {
System.out.println("子类汽车引擎~~~~~");
}
}
public class Bike extends AbstractCar {
private int lunzi;
@Override
public void move() {
this.lunzi=2;
System.out.println("自行车轮子:"+this.lunzi);
super.yinqing();
this.yinqing();
// TODO Auto-generated method stub
}
public void yinqing(){
System.out.println("单车引擎,哈哈!");
}
}
[color=red]接口[/color]
public interface IDoor {
public void alarm();
}
[color=red]
TestA类[/color]
public class TestA {
public void close() {
// TODO Auto-generated method stub
System.out.println("关门。。。。。");
}
public void open() {
// TODO Auto-generated method stub
System.out.println("开门。。。。");
}
}
[color=red]TestB类[/color]
public class TestB extends TestA implements IDoor{
public void open(){
System.out.println("子类开门.....");
}
public void close(){
System.out.println("子类关门.....");
}
public void alarm(){
super.open();
this.open();
super.close();
this.close();
System.out.println("接口警报。。。。。");
}
}
[color=red]抽象类:[/color]
/**
* 抽象类一般声明这个类所拥有的基本特征(属性)和行为(方法)
* 里面可以有非抽象的方法
*
*/
public abstract class AbstractCar {
/**
* 抽象类中可以有抽象方法,也可以有其他方法
*/
public int lunzi = 2;// 轮子
// 抽象方法不需要{},由子类中具体实现
public abstract void move(); // 移动方法
// 不是抽象方法就需要{}
public void yinqing() {
System.out.println("抽象类中引擎类型方法:");
};
}
[color=red]以下是抽象类的两个子类:[/color]
public class Car extends AbstractCar {
/**
* 如果子类中没有定义与父类相同的属性,this.xx调用的父类中的属性 此时 给this.xx或super.xx的属性赋值都是给父类属性重新赋值
*
*/
private int lunzi;
/**
*
* move方法是父类抽象方法的具体实现
*/
@Override
public void move() {
this.lunzi = 4;// 本类如果没声明lunzi属性就会重新给父类lunzi赋值
System.out.println(this.lunzi);
System.out.println("父类默认属性:" + super.lunzi + "只轮子");
// TODO Auto-generated method stub
System.out.println("我是汽车,要跑了!");
super.yinqing();
this.yinqing();
}
public void yinqing() {
System.out.println("子类汽车引擎~~~~~");
}
}
public class Bike extends AbstractCar {
private int lunzi;
@Override
public void move() {
this.lunzi=2;
System.out.println("自行车轮子:"+this.lunzi);
super.yinqing();
this.yinqing();
// TODO Auto-generated method stub
}
public void yinqing(){
System.out.println("单车引擎,哈哈!");
}
}
[color=red]接口[/color]
public interface IDoor {
public void alarm();
}
[color=red]
TestA类[/color]
public class TestA {
public void close() {
// TODO Auto-generated method stub
System.out.println("关门。。。。。");
}
public void open() {
// TODO Auto-generated method stub
System.out.println("开门。。。。");
}
}
[color=red]TestB类[/color]
public class TestB extends TestA implements IDoor{
public void open(){
System.out.println("子类开门.....");
}
public void close(){
System.out.println("子类关门.....");
}
public void alarm(){
super.open();
this.open();
super.close();
this.close();
System.out.println("接口警报。。。。。");
}
}