我是靠谱客的博主 细腻鸵鸟,这篇文章主要介绍Javase day05_作业作业,现在分享给大家,希望可以做个参考。

作业

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/** * 1、定义一个学生类,包括属性:学号(ID),姓名(name), * 成绩(score);构造方法(带三个参数);每个属性的访问器方法。 * */ public class Students { private int ID; private String name; private int score; public Students() { super(); } public Students(int iD, String name, int score) { super(); ID = iD; this.name = name; this.score = score; } public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
复制代码
1
2
3
4
5
6
@Test public void myTest01(){ Students s=new Students(1234,"yunjiaen",150); System.out.println(s.getID()+" "+s.getName()+" "+s.getScore()); }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/** * 2、创建类A1,实现构造方法中输出This is A;创建A1的子类B1, * 实现构造方法中输出This is B;创建B1的子类C1,实现构造方法中输出This is C。 */ public class A1 { public A1() { super(); System.out.println("This is A"); } }
复制代码
1
2
3
4
5
6
7
public class B1 extends A1{ public B1() { super(); System.out.println("This is B"); } }
复制代码
1
2
3
4
5
6
7
public class C1 extends B1 { public C1() { super(); System.out.println("This is C"); } }
复制代码
1
2
3
4
5
6
7
@Test public void myTest02(){ A1 a=new A1(); B1 b=new B1(); C1 c=new C1(); }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/** * 3、设计四个类,分别如下: [必做题] * 3.1 设计Shape表示图形类, * 有面积属性area、周长属性per,颜色属性color,有两个构造方法 * (一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、 * getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。 * 3.2 设计 2个子类: 3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度、 * height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法 * (一个是默认的、一个是为高度、宽度、颜色赋值的)。 3.2.2 Circle表示圆类,增加1个属性 * ,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、 * 颜色赋值的)。 * 3.3 测试类中,在main方法中, * 声明创建每个子类的对象,并调用2个子类的showAll方法。 */ public abstract class Shape { private double area; private double per; private String color; public Shape() { this.area=0; this.per=0; this.color="白色"; } public Shape(String color) { super(); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public abstract double getArea(); public abstract double getPer(); public abstract void showAll(); }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Rectangle extends Shape { private double Width; private double height; public Rectangle() { this.Width=0; this.height=0; } public Rectangle(String color,double width, double height) { setColor(color); this.Width = width; this.height = height; } @Override public double getArea() { double area=this.Width*this.height; return area; } @Override public double getPer() { double per=2*(this.Width+this.height); return per; } @Override public void showAll() { System.out.println("图形为矩形"); System.out.println("长度为:"+this.height); System.out.println("宽度为:"+this.Width); System.out.println("面积为:"+this.getArea()); System.out.println("周长为:"+this.getPer()); System.out.println("颜色为:"+this.getColor()); } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Circle extends Shape { private double radius; public Circle() { super(); radius=0; } public Circle(String color,double radius) { setColor(color); this.radius = radius; } @Override public double getArea() { double area=3.14*this.radius*this.radius; return area; } @Override public double getPer() { double per=2*this.radius*3.14; return per; } @Override public void showAll() { System.out.println("图形为矩形"); System.out.println("半径为:"+this.radius); System.out.println("面积为:"+this.getArea()); System.out.println("周长为:"+this.getPer()); System.out.println("颜色为:"+this.getColor()); } } @Test public void myTest03() { Rectangle r=new Rectangle("蓝色",5.0,4.0); r.showAll(); Circle c=new Circle("红色",3.0); c.showAll(); } }

最后

以上就是细腻鸵鸟最近收集整理的关于Javase day05_作业作业的全部内容,更多相关Javase内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部