java——抽象类的实例化(限制子类访问)

2019年2月16日10:15:43 发表评论 959 views
/**
 * Created by xabcd on 2019/2/16.
 */
abstract class java_chouxiang1
{String name;
    int age;
    String occupation;
    //声明一种抽象方法talk()
    public abstract String talk();
}
//Student4类继承自java_chouxiang1类
class Student4 extends java_chouxiang1
{
    public Student4(String name,int age,String occuupation)
    {
        this.name = name;
        this.age = age;
        this.occupation= occuupation;
    }
    //覆写talk()方法,此类不为抽象类,所以需要覆写
    public String talk()
    {
        return "学生——》姓名:"+this.name+",年龄:"+this.age+"职业:"+this.occupation;
    }
}
//worker类继承自java_chouxiang1类
class Worker extends java_chouxiang1
{
    public Worker(String name,int age,String occuupation)
    {
        this.name = name;
        this.age = age;
        this.occupation= occuupation;
    }
    public String talk()//此类不为抽象类,所以需要覆写
    {
        return "工人——》姓名:"+this.name+",年龄:"+this.age+"职业:"+this.occupation;
    }
}








/**
 * Created by xabcd on 2019/2/16.
 */
public class test_chouxiang
{
    public static void main(String[] args)
    {
        Student4 s = new Student4("张三",20,"学生");
        Worker w = new Worker("李四",30,"工人");
        System.out.println(s.talk());
        System.out.println(w.talk());
    }
}




学生——》姓名:张三,年龄:20职业:学生
工人——》姓名:李四,年龄:30职业:工人

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: