标签:提示 teacher [] person == 继承 new 计算 ble
1)声明一个Person类,有name(String类型)、age(int类型)、sex(char类型)属性,通过构造方法进行赋值。一个show方法,返回String类型,内容如下:
某某 男(女) 年龄
2)声明一个Student类,继承Person类,增加id(int,学号)属性,通过构造方法,利用super调用父类构造方法来进行变量赋值。Override父类的show方法,返回String类型,内容如下:
某某 男(女) 年龄 学号 (提示:利用super调用父类的show方法得到除学号部分的String,然后加上学号的信息。)
3)声明一个Teacher类,继承Person,增加course(String,所教课程)属性,通过构造方法,利用super调用父类构造方法来进行变量赋值。Override父类的show方法,返回String类型,内容如下:
某某 男(女)年龄 所教课程 (提示:利用super调用父类的show方法得到除所教课程部分的String,然后加上所教课程的信息。)
4)声明PersonApp类,在其中的main方法中分别声明Person、Student、Teacher类型的变量,并通过构造方法初始化,然后显示各自的信息。
1 public class PersonApp{
2 public static void main(String[] ages){
3 Person p=new Person("张三",‘男‘,20);
4 Person s=new Student("李四",‘男‘,21,16040210);
5 Person t=new Teacher("王五",‘女‘,22,"数学");
6 System.out.println(p.show());
7 System.out.println(s.show());
8 System.out.println(t.show());
9 }
10 }
11 class Person{
12 private String name;
13 private char sex;
14 private int age;
15 Person(String name,char sex,int age){
16 this.name=name;
17 this.sex=sex;
18 this.age=age;
19 }
20 public String getName(){
21 return name;
22 }
23 public void setName(String name){
24 this.name=name;
25 }
26 public char getSex(){
27 return sex;
28 }
29 public void setSex(char sex){
30 if(sex==‘男‘||sex==‘女‘)
31 this.sex=sex;
32 else
33 System.out.println("sex error!");
34 }
35 public int getAge(){
36 return age;
37 }
38 public void setAge(int age){
39 if(age>0&&age<120)
40 this.age=age;
41 else
42 System.out.println("age error!");
43 }
44 public String show(){
45 return name+","+sex+","+age;
46 }
47 }
48 class Student extends Person{
49 private int id;
50 Student(String name,char sex,int age,int id){
51 super(name,sex,age);
52 this.id=id;
53 }
54 public String show(){
55 return super.show()+","+id;
56 }
57 }
58 class Teacher extends Person{
59 private String course;
60 Teacher(String name,char sex,int age,String course){
61 super(name,sex,age);
62 this.course=course;
63 }
64 public String show(){
65 return super.show()+" "+course;
66 }
67 }
声明一个Shape接口,其中有计算面积(area)、周长(perimeter)的方法,有以下几个实现:Circle(圆),Rectangle(矩形),Triangle(三角形),都有计算面积、周长的方法。
1 interface Shape
2 {
3 void count();
4 }
5 class Circle implements Shape
6 {
7 private int r;
8 private double CirclePerimeter;
9 Circle(int r)
10 {
11 this.r=r;
12 }
13 public void count()
14 {
15 if(r<0||r==0)
16 {
17 System.out.println("r error!");
18 return;
19 }
20 else
21 CirclePerimeter=2*r*3.14;
22 double CircleArea=3.14*r*r;
23 System.out.println("圆的周长是:"+CirclePerimeter+",面积是 "+CircleArea);
24 }
25 }
26 class Rectangle implements Shape
27 {
28 private double l,w;
29 private double RectanglePerimeter;
30 Rectangle(double l,double w)
31 {
32 this.l=l;
33 this.w=w;
34 }
35 public void count()
36 {
37 if((l>0)&&(w>0))
38 {
39 RectanglePerimeter=2*(l+w);
40 double RectangleArea=l*w;
41 System.out.println("矩形的周长是:"+RectanglePerimeter+",面积是"+RectangleArea);
42 }
43 else
44 {
45 System.out.println("l or w error!");
46 }
47
48 }
49 }
50 class Triangle implements Shape
51 {
52 private double d,h,s1,s2;
53 Triangle(double d,double h,double s1,double s2)
54 {
55 this.d=d;
56 this.h=h;
57 this.s1=s1;
58 this.s2=s2;
59 }
60 public void count()
61 {
62 if((d>0)&&(s1>0)&&(s2>0)&&(h>0))
63 {
64 double TrianglePerimeter=d+s1+s2;
65 double TriangleArea=(d*h)/2;
66 System.out.println("三角形的周长是:"+TrianglePerimeter+",面积是"+TriangleArea);
67 }
68 else
69 {
70 System.out.println("d or h or s1 or s2 error!");
71 }
72 }
73 }
74 public class Show
75 {
76 public static void main(String[] args)
77 {
78 Shape a1=new Circle(3);
79 Shape a2=new Rectangle(4,5);
80 Shape a3=new Triangle(8,3,5,5);
81 a1.count();
82 a2.count();
83 a3.count();
84 }
85 }
标签:提示 teacher [] person == 继承 new 计算 ble
原文地址:https://www.cnblogs.com/TimhLiu/p/9245809.html