当前位置:网站首页>3. abstract class (shape)
3. abstract class (shape)
2022-06-22 16:18:00 【Xiaomurong】
3 abstract class
Define an abstract class shape, Including the public calculation area area Method , Calculate the volume volume Method , Output basic information method printinfo( All three methods are abstract methods ).
from shape The derived point class , Add private data members x,y coordinate , And the construction method . from point The derived circle class , Increase the radius of private data members r, And the construction method .
from circle The derived cylinder class , Increase the height of private data members h, And the construction method .
stay main In the method , Definition shape Class object , Objects that reference derived classes , Output the basic information of three types of objects , area , Volume .
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());
}
}
边栏推荐
- mysql - sql执行过程
- nvarchar和varchar的区别
- What is the relationship between CSC securities and qiniu school? Is it safe to open a securities account
- 音视频基础知识|ANS 噪声抑制原理解析
- [Shanda conference] software performance optimization and bug repair
- SAP ABAP 子屏幕教程:在 SAP 中调用子屏幕-010
- 跨界融合创意创新,助力提高文旅夜游影响力
- [Shanda conference] project introduces Redux
- [译文] 弥合开源数据库和数据库业务之间的鸿沟
- 【山大会议】项目初始化
猜你喜欢
随机推荐
wallys/WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz
Make the text template in pycharm project support jinjia2 syntax
【华为云至简致远】征文获奖名单出炉!
【山大会议】多人视频通话 WebRTC 工具类搭建
GD32F4xx MCU 驱动mcp2515扩展CAN接口
[Shanda conference] software performance optimization and bug repair
#进程地址空间
知识管理在业务中的价值如何体现
数字人民币可以买理财产品了!建行APP在试点地区上线服务专区,实测体验如何?
Jenkins 通过检查代码提交自动触发编译
微信小程序头像挂件制作
[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want
5.文件的读写(学生类)
程序替换函数
Rosbag使用命令
stack和queue的模拟实现
nvarchar和varchar的区别
Deploy odoo to the server and configure it as a service
【山大会议】项目初始化
【山大会议】WebRTC基础之对等体连接








