欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

设计一个限制子类的访问的抽象类实例,要求在控制台输出如下结果

程序员文章站 2022-03-18 14:05:41
1 public class TEST { 2 3 public static void main(String[] args) { 4 Teacher t1=new Teacher(50,"刘三","教师"); 5 Worker t2=new Worker(30,"赵四","工人"); 6 t1. ......

 

 1 public class test {
 2 
 3 public static void main(string[] args) {
 4 teacher t1=new teacher(50,"刘三","教师");
 5 worker t2=new worker(30,"赵四","工人");
 6 t1.talk();
 7 t2.talk();
 8 
 9 }
10 
11 }
12 abstract class person{
13 private string name;
14 private int age;
15 
16 public person(int age,string name){
17 this.age=age;
18 this.name=name;
19 }
20 public string getname() {
21 return name;
22 }
23 public int getage() {
24 return age;
25 }
26 }
27 class teacher extends person{
28 string job="教师";
29 public teacher(int age,string name,string job){
30 super(age,name);
31 this.job=job;
32 }
33 public void talk() {
34 system.out.println("教师->姓名:"+getname()+",年龄,"+getage()+"职业:"+job);
35 }
36 }
37 class worker extends person{
38 string job="工人";
39 public worker(int age,string name,string job){
40 super(age,name);
41 this.job=job;
42 }
43 public void talk() {
44 system.out.println("工人->姓名:"+getname()+",年龄,"+getage()+"职业:"+job);
45 }
46 }