当前位置:网站首页>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());
}
}
边栏推荐
- Deploy odoo to the server and configure it as a service
- 19、 Xv6 context switching (implementation of context switching; encapsulation and recovery of state machine)
- SAP ABAP 中的模块化:宏、子程序和功能模块 -04
- nvarchar和varchar的区别
- SLAM十四讲之第6讲--非线性优化
- Navicat premium connecting to Oracle database (Graphic tutorial)
- webDriver以及Selenium使用总结
- stack和queue的模拟实现
- Unity game optimization (version 2) learning record 8
- 着力打造网红产品,新捷途X70S焕新上市,8.79万起售
猜你喜欢
![[leetcode] 9. Palindromes](/img/58/1817b072949458f9652c144ac4ec0e.png)
[leetcode] 9. Palindromes

Pymssql Module User Guide

Focus on creating a net red product. The xinjietu x70s is newly launched, starting from 87900

C language learning -18-makefile file writing examples and how to generate and call dynamic libraries

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

odoo本地文档功能开发记录

SAP abap 数据类型,操作符和编辑器-02
![[Shanda conference] application setting module](/img/1e/1665234715b365614a753355274ced.png)
[Shanda conference] application setting module

B树和B+树

数字人民币可以买理财产品了!建行APP在试点地区上线服务专区,实测体验如何?
随机推荐
odoo部署到服务器并配置为服务
SLAM十四讲之第6讲--非线性优化
Simulation of vector
[Shanda conference] use typescript to reconstruct the project
Runtime——探索类,对象,分类本质
[leetcode] 9. Palindromes
转:杰克·韦尔奇:战略就是要少点沉思,敏于行动
Cve-2022-0847 (privilege lifting kernel vulnerability)
实现一个Container全局组件步骤(给还不会使用组件的新手一个思路,大佬绕道)
[译文] 弥合开源数据库和数据库业务之间的鸿沟
Runtime -- explore the nature of classes, objects, and classifications
【山大会议】一些基本工具类定义
SAP 脚本教程:SE71、SE78、SCC1、VF03、SO10-013
Odoo local document function development record
静态断言 static_assert
【单片机】【让蜂鸣器发声】认识蜂鸣器,让蜂鸣器发出你想要的声音
解决mysql远程登录报权限问题
各位学弟学妹,别再看教材了,时间复杂度看这篇就好了
SAP abap 数据类型,操作符和编辑器-02
Oracle客户端和服务端的区别