当前位置:网站首页>3.抽象类(shape)
3.抽象类(shape)
2022-06-22 15:02: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());
}
}
边栏推荐
- String的模拟实现
- Quickly play ci/cd graphical choreography
- 着力打造网红产品,新捷途X70S焕新上市,8.79万起售
- 【山大会议】WebRTC基础之用户媒体的获取
- 数值类型和字符串之间的转换
- 数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
- Rosbag使用命令
- Dear students, don't read the textbooks any more. Just read this one for the complexity of time
- SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017
- ORB_ VI ideological framework
猜你喜欢

Program substitution function

POD 类型
The MIHA tour club in June is hot! 500+ posts, more than HC, just this summer (with internal promotion method)
![[leetcode] 9. Palindromes](/img/58/1817b072949458f9652c144ac4ec0e.png)
[leetcode] 9. Palindromes

【一起上水硕系列】Day Three - video

Dear students, don't read the textbooks any more. Just read this one for the complexity of time

Jenkins 通过检查代码提交自动触发编译

ironSource Luna 推出苹果搜索广告限时优惠,注册即享3个月免费服务

静态断言 static_assert

Pymssql Module User Guide
随机推荐
POD 类型
School enterprise alliance is on the way! Huawei cloud gaussdb has come to universities again
Navicat premium connecting to Oracle database (Graphic tutorial)
【山大会议】多人视频通话 WebRTC 工具类搭建
[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want
[Shanda conference] software performance optimization and bug repair
【单片机】【让蜂鸣器发声】认识蜂鸣器,让蜂鸣器发出你想要的声音
数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
B树和B+树
中信建投证券是跟启牛学堂存在什么关系?开证券账户安全吗
Linux安装mysql
Discourse 新用户可插入媒体的数量
What is the relationship between CSC securities and qiniu school? Is it safe to open a securities account
Odoo local document function development record
Mr. Du built a domestic non garlic Statistics Platform
String的模拟实现
nvarchar和varchar的区别
[Shangshui Shuo series] day three - VIDEO
Gbase "library" special training of innovation and application Committee of Beijing fintech Industry Alliance
ORB_ VI ideological framework