当前位置:网站首页>3.抽象類(shape)
3.抽象類(shape)
2022-06-22 16:17:00 【小木榮】
3 抽象類
定義一個抽象類shape,包括公有的計算面積area方法,計算體積volume方法,輸出基本信息方法printinfo(三個方法均為抽象方法)。
從shape派生point類,增加私有數據成員x,y坐標,以及構造方法。從point派生circle類,增加私有數據成員半徑r,以及構造方法。
從circle派生cylinder類,增加私有數據成員高度h,以及構造方法。
在main方法中,定義shape類的對象,引用派生類的對象,輸出三類對象的基本信息,面積,體積。
public class Test {
public static void main(String[] args) {
System.out.println("Point");
shape sp1 = new Point(2, 2);
sp1.printinfo();
System.out.println("Circle");
shape sp2 = new Circle(2);
sp2.printinfo();
sp2.area();
System.out.println("Cylinder");
shape sp3 = new Cylinder(2);
sp3.printinfo();
sp3.volume();
}
}
abstract class shape {
abstract void area();
abstract void printinfo();
abstract void volume();
}
class Point extends shape {
private int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
int getx() {
return x;
}
int gety() {
return y;
}
@Override
void area() {
// TODO Auto-generated method stub
}
@Override
void printinfo() {
// TODO Auto-generated method stub
System.out.println(getx() + "," + gety());
}
@Override
void volume() {
// TODO Auto-generated method stub
}
}
class Circle extends Point {
private int r;
public Circle(int r) {
super(2, 2);
this.r = r;
}
int getr() {
return r;
}
void area() {
System.out.println("area:" + r * r * 3.14);
}
void printinfo() {
System.out.println(getx() + "," + gety() + "," + getr());
}
}
class Cylinder extends Circle {
private int h;
Cylinder(int h) {
super(2);
this.h = h;
}
int geth() {
return h;
}
void printinfo() {
System.out.println(getx() + "," + gety() + "," + getr() + "," + geth());
}
void volume() {
System.out.println("volume:" + 3.14 * Math.pow(super.getr(), 2) * geth());
}
}
边栏推荐
- 洛谷P2466 [SDOI2008] Sue 的小球 题解
- 各位学弟学妹,别再看教材了,时间复杂度看这篇就好了
- [Huawei cloud to Jian Zhiyuan] the winning list of essay solicitation is released!
- Rosbag use command
- Maze problem (BFS record path)
- 5.文件的读写(学生类)
- High precision calculation
- 校企联合在路上!华为云GaussDB又来高校啦
- GD32F4xx MCU 驱动mcp2515扩展CAN接口
- 中信建投证券是跟启牛学堂存在什么关系?开证券账户安全吗
猜你喜欢
随机推荐
SAP abap 数据类型,操作符和编辑器-02
Deploy odoo to the server and configure it as a service
[Shanda conference] use typescript to reconstruct the project
SAP ABAP 报告编程-08
uni开发微信小程序自定义相机自动检测(人像+身份证)
SAP ABAP 对话框编程教程:中的模块池-09
默认函数控制 =default 与 =delete
jmeter关联登录302类型的接口
Program substitution function
[Shangshui Shuo series] day three - VIDEO
SAP ABAP 子屏幕教程:在 SAP 中调用子屏幕-010
phantomJs使用总结
解决mysql远程登录报权限问题
Turn to: jackwelch: strategy is to think less and be quick to act
MySQL trigger
Odoo local document function development record
程序替换函数
pymssql模块使用指南
SAP 脚本教程:SE71、SE78、SCC1、VF03、SO10-013
POD 类型
![[leetcode] 9. Palindromes](/img/58/1817b072949458f9652c144ac4ec0e.png)







