当前位置:网站首页>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());
}
}
边栏推荐
- 【LeetCode】9、回文数
- 【一起上水硕系列】Day Three - video
- Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour
- SAP ABAP 子屏幕教程:在 SAP 中调用子屏幕-010
- DDD understanding of Domain Driven Design
- odoo系统对原有模型单独开发的视图设置优先级
- [Shanda conference] definitions of some basic tools
- 【单片机】【让蜂鸣器发声】认识蜂鸣器,让蜂鸣器发出你想要的声音
- GD32F4xx MCU 驱动mcp2515扩展CAN接口
- #进程地址空间
猜你喜欢
![[Shanda conference] application setting module](/img/1e/1665234715b365614a753355274ced.png)
[Shanda conference] application setting module

Discourse 新用户可插入媒体的数量

Bridging the gap between open source databases and database services

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

微信小程序头像挂件制作
![[Shangshui Shuo series] day three - VIDEO](/img/42/0911fee2a36f6dda345a571a31acd5.png)
[Shangshui Shuo series] day three - VIDEO

Jenkins automatically triggers compilation by checking code submissions

Gbase "library" special training of innovation and application Committee of Beijing fintech Industry Alliance

信创研究:国产数据库聚焦信创市场,华为Gauss有望成为最强

【单片机】【让蜂鸣器发声】认识蜂鸣器,让蜂鸣器发出你想要的声音
随机推荐
SAP价值流程&帮助请求流程-011
Quick sort_ sort
Dear students, don't read the textbooks any more. Just read this one for the complexity of time
[Shanda conference] establishment of webrtc tools for multi person video call
SAP ABAP BAPI-016
SAP ABAP 报告编程-08
4.字符串(倒序且大小写转换)
Unity游戏优化(第2版)学习记录8
6.GUI(图形,填充)
华为云HCDEZ专场暨分布式技术峰会:华为云分布式云原生技术与实践之路
The odoo system sets priorities for the views independently developed by the original model
Scala language learning-06-differences between name passing parameters, value passing parameters and function passing parameters
C language learning -18-makefile file writing examples and how to generate and call dynamic libraries
Discourse 新用户可插入媒体的数量
阿里云中间件的开源往事
【山大会议】使用TypeScript为项目进行重构
Runtime——探索类,对象,分类本质
Mr. Du built a domestic non garlic Statistics Platform
Discover the number of media new users can insert
SAP教程中的ALV报告 - ABAP列表查看器-012