当前位置:网站首页>3. Classe abstraite (forme)
3. Classe abstraite (forme)
2022-06-22 16:17:00 【Xiao Murong.】
3 Classe abstraite
Définir une classe abstraiteshape,Y compris la zone de calcul publiqueareaMéthodes,Calculer le volumevolumeMéthodes,Méthode de sortie des informations de baseprintinfo(Les trois méthodes sont abstraites).
DeshapeDérivationpointCatégorie,Ajouter des membres de données privéesx,yCoordonnées,Et la méthode de construction.DepointDérivationcircleCatégorie,Augmenter le rayon des membres de données privésr,Et la méthode de construction.
DecircleDérivationcylinderCatégorie,Augmenter la hauteur des membres de données privésh,Et la méthode de construction.
InmainDans la méthode,DéfinitionshapeObjet de la classe,Objet faisant référence à une classe dérivée,Exporter des informations de base sur trois types d'objets,Superficie,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());
}
}
边栏推荐
- Differences between Oracle client and server
- How can ordinary people make 1million yuan a year?
- 信创研究:国产数据库聚焦信创市场,华为Gauss有望成为最强
- C language learning -17- function is passed in as a parameter
- 【山大会议】项目引入 Redux
- Bridging the gap between open source databases and database services
- [single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want
- 华为云HCDEZ专场暨分布式技术峰会:华为云分布式云原生技术与实践之路
- Rosbag使用命令
- How to embody the value of knowledge management in business
猜你喜欢

mysql - sql执行过程

让pycharm项目里面的文本模板支持jinjia2语法

Navicat Premium 连接Oracle 数据库(图文教程)

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

SAP abap 数据类型,操作符和编辑器-02

数睿数据深度 | 关于软件自主可控,源代码向左,无代码向右

数值类型和字符串之间的转换

Wechat applet avatar pendant production

POD 类型

The odoo system sets priorities for the views independently developed by the original model
随机推荐
String的模拟实现
校企联合在路上!华为云GaussDB又来高校啦
畅享高性能计算!天翼云HPC解决方案来了
[Shanda conference] establishment of webrtc tools for multi person video call
odoo本地文档功能开发记录
Scala language learning-05-a comparison of the efficiency of recursion and tail recursion
默认函数控制 =default 与 =delete
Discourse 新用户可插入媒体的数量
Discourse 的信任级别
(pytorch advanced path 2) word embedding and position embedding
[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want
How safe is the new bond
Jenkins 通过检查代码提交自动触发编译
MySQL trigger
Batch export excel zip using zipfile, openpyxl and flask
Program substitution function
How can ordinary people make 1million yuan a year?
#进程地址空间
Oracle客户端和服务端的区别
Huawei cloud hcdez special session and Distributed Technology Summit: Huawei cloud distributed cloud native technology and Practice