当前位置:网站首页>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());
}
}
边栏推荐
猜你喜欢

uni开发微信小程序自定义相机自动检测(人像+身份证)

微信小程序头像挂件制作

跨界融合创意创新,助力提高文旅夜游影响力

Quickly play ci/cd graphical choreography

Uni develops wechat applet to customize automatic camera detection (portrait + ID card)

SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017

odoo系统对原有模型单独开发的视图设置优先级

Unity game optimization (version 2) learning record 8

程序替换函数

jmeter关联登录302类型的接口
随机推荐
Solve the problem of MySQL remote login permission
解决mysql远程登录报权限问题
SAP ABAP 中的 Smart Forms-014
SAP 脚本教程:SE71、SE78、SCC1、VF03、SO10-013
Runtime——探索类,对象,分类本质
MySQL trigger
SAP ABAP 子屏幕教程:在 SAP 中调用子屏幕-010
(pytorch advanced path 2) word embedding and position embedding
Alibaba cloud middleware's open source past
数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
默认函数控制 =default 与 =delete
Dear students, don't read the textbooks any more. Just read this one for the complexity of time
Luogu p2466 [sdoi2008] Sue's small ball solution
uni开发微信小程序自定义相机自动检测(人像+身份证)
SAP教程中的ALV报告 - ABAP列表查看器-012
SAP ABAP 中的模块化:宏、子程序和功能模块 -04
[VTK] model rotation and Translation
各位学弟学妹,别再看教材了,时间复杂度看这篇就好了
【山大会议】注册页的编写
[Shanda conference] definitions of some basic tools