当前位置:网站首页>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());
}
}
边栏推荐
- B树和B+树
- [Huawei cloud to Jian Zhiyuan] the winning list of essay solicitation is released!
- [Shanda conference] project initialization
- CMake教程系列-00-简介
- School enterprise alliance is on the way! Huawei cloud gaussdb has come to universities again
- SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017
- 【单片机】【让蜂鸣器发声】认识蜂鸣器,让蜂鸣器发出你想要的声音
- 【山大会议】多人视频通话 WebRTC 工具类搭建
- ORB_VI思想框架
- 【山大会议】WebRTC基础之对等体连接
猜你喜欢
随机推荐
New design of databend SQL planner
The difference between nvarchar and varchar
【山大会议】项目引入 Redux
[Shanda conference] application setting module
[Shangshui Shuo series] day three - VIDEO
在JFlash中添加未知类型的单片机
Batch export excel zip using zipfile, openpyxl and flask
B树和B+树
pymssql模块使用指南
What is the relationship between CSC securities and qiniu school? Is it safe to open a securities account
SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017
Differences between Oracle client and server
Rosbag use command
odoo本地文档功能开发记录
Trust level of discover
畅享高性能计算!天翼云HPC解决方案来了
Discover the number of media new users can insert
知识管理在业务中的价值如何体现
[Shanda conference] peer connection based on webrtc
MySQL trigger


![[leetcode] 9. Palindromes](/img/58/1817b072949458f9652c144ac4ec0e.png)






