package Diqizhang;
public class Base{
public Base(){
System.out.println( "Base");
}
}
class Child extends Base{
public Child(){
System.out.println("Child");
}
}
package Diqizhang;
public class Sample{
public static void main(String[] args){
Child c=new Child();
}
}
运行结果:
Base
Child
进程已结束,退出代码为 0
Animal
package Diqizhang;
public class Animal {
private int age = 0;//年龄
public Animal() {
}
public Animal(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void info(){
System.out.println("我今年" + this.getAge() + "岁了!");
}
}
Bird
package Diqizhang;
public class Bird extends Animal {
private String color = "红色";//颜色
public Bird(int age, String color) {
super(age);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void info() {
System.out.println("我是一只" + color + "的鸟!");
super.info();
System.out.println();
}
}
Fish
public class Fish extends Animal {
private int weight = 4;//重量
public Fish(int age, int weight) {
super(age);
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void info() {
System.out.println("我是一只" + weight + "斤重的鱼!");
super.info();
System.out.println();
}
}
测试类:
package Diqizhang;
public class Test {
public static void main(String[] args) {
Bird bird = new Bird(4, "红色");
Fish fish = new Fish(2, 5);
bird.info();
fish.info();
}
}
运行结果:
我是一只红色的鸟!
我今年4岁了!
我是一只5斤重的鱼!
我今年2岁了!
进程已结束,退出代码为 0
public class APhone extends Phones {
/**
* 打电话
*/
public void call() {
System.out.println("安卓手机打电话");
}
}
public class IPhone extends Phones {
/**
* 打电话
*/
public void call() {
System.out.println("苹果手机打电话");
}
}
public abstract class Phones {
/**
* 抽象方法,实现打电话。
*/
public abstract void call();
}
public class TestCall {
public static void main(String[] args) {
//创建安卓手机对象并打电话
Phones aPhone=new APhone();
aPhone.call();
//创建苹果手机对象并打电话
Phones iPhone=new IPhone();
iPhone.call();
//创建windows Phone对象并打电话
Phones wPhone=new WPhone();
wPhone.call();
}
}
public class WPhone extends Phones {
/**
* 打电话
*/
public void call() {
System.out.println("WPhone手机打电话");
}
}
运行结果:
安卓手机打电话
苹果手机打电话
WPhone手机打电话
进程已结束,退出代码为 0