java中继承的必要性
/**
*说明继承的必要性
*2012-3-13
*/
class Inherit
{
public static void main(String[] args)
{
Pupil p1=new Pupil();
p1.Fee(12);
p1.showFee(12);
}
}
/**
*将学生的共有属性抽象出来
*/
class Stu
{
//定义成员属性
public int age;
public String name;
public float fee;
public void showFee(float fee)
{
System.out.println(fee);
}
}
/**
*小学生
*/
class Pupil extends Stu
{
/*
private int age;
private String name;
private float fee;
*/
public void Pupil(int age,String name)
{
this.age=age;
this.name=name;
}
public void Fee(int fee)
{
this.fee=fee*0.8f;
}
}
/**
*中学生
*/
class MidileStu extends Stu
{
/*
private int age;
private String name;
private float fee;
*/
public void MidileStu(int age,String name)
{
this.age=age;
this.name=name;
}
public void Fee(int fee)
{
this.fee=fee*0.8f;
}
}
/**
*大学生
*/
class CollegeStu extends Stu
{
/*
private int age;
private String name;
private float fee;
*/
public void CollegeStu(int age,String name)
{
this.age=age;
this.name=name;
}
public void Fee(int fee)
{
this.fee=fee*0.8f;
}
}