java新手笔记16 面积
1.图形类
package com.yfs.javase;
public class Shape {
//计算面积方法
public double getArea() {
System.out.println("计算面积");
return 0;
}
}
2.圆
package com.yfs.javase;
public class Circle extends Shape {
private double r;
public Circle(double r) {
this.r = r;
System.out.println("创建圆形面积");
}
public double getArea() {//覆盖父类的方法
System.out.println("计算圆形面积...");
return 3.14 * r * r;
}
}
3.矩形
package com.yfs.javase;
public class Rangton extends Shape {
private double width;
private double length;
public Rangton(double width, double length) {
this.width = width;
this.length = length;
System.out.println("创建矩形面积");
}
public double getArea() {
System.out.println("计算矩形面积...");
return width * length;
}
}
4.三角形
package com.yfs.javase;
public class Trantangle extends Shape {
private double height;
private double width;
public Trantangle(double height, double width) {
this.height = height;
this.width = width;
System.out.println("创建三角形面积");
}
public double getArea() {
System.out.println("计算三角形面积...");
return 1.0 / 2 * width * height;
}
}
5.测试
package com.yfs.javase;
import java.util.Random;
public class Test1 {
/**
* 编写一个图形类,提供计算面积的方法。
* 通过继承图形类,封装三角形,圆形,正方形类,
* 覆盖父类的方法。在测试类里随机产生10个图形,
* 面积求和。
*/
public static void main(String[] args) {
Shape[] shapes = new Shape[10];//存放子类对象
Random ran = new Random();
double sum = 0;
//创建随即图形
for (int i = 0; i < shapes.length; i++) {
int r = ran.nextInt(101);
if(r > 65) {
shapes[i] = new Circle(ran.nextInt(10));
} else if( r > 35 ){
shapes[i] = new Rangton(ran.nextInt(10),ran.nextInt(10));
//shapes[i].setWidth();
} else {
shapes[i] = new Trantangle(ran.nextInt(10), ran.nextInt(10));
}
}
System.out.println("================");
//计算随机图形面积
for (int i = 0; i < shapes.length; i++) {
// Circle c = (Circle)shapes[i];
// sum += c.getArea();
sum += shapes[i].getArea();//子类对象计算面积
}
System.out.println("sum = " + sum);
}
}
java新手笔记16 面积的更多相关文章
- java新手笔记24 Math/String对象
1.Math package com.yfs.javase; public class MathDemo { public static void main(String[] args) { int ...
- java新手笔记32 jdk5新特性
1.for package com.yfs.javase; import java.awt.Color; import java.util.Calendar; import java.util.Has ...
- java新手笔记26 Frame
0.Calculater package com.yfs.javase; import java.awt.BorderLayout; import java.awt.Button; import ja ...
- java新手笔记18 类比较
1.Shap类 package com.yfs.javase; public class Shape /*extends Object */{ //默认继承object object方法全部继承 // ...
- java新手笔记5 类
1.进制转换 /* 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时, 低于10万元的部分按10%提成,高于10万元的部分,可提成7.5 ...
- java新手笔记3 运算符&循环
1.包 2.运算符 public class Operator { public static void main(String[] args) { int a = 5; System.out.pri ...
- JAVA新手笔记 Intent对象和Bundle对象
Intent对象和Bundle对象 功能主要是在 MainActivity中定义了2个EditText,当用户输入内容,把他传入到第二个活动, 自己新创的活动中,MyActivity中 放在MainA ...
- java新手笔记34 连接数据库
1.JdbcUtil package com.yfs.javase.jdbc; import java.sql.Connection; import java.sql.DriverManager; i ...
- java新手笔记33 多线程、客户端、服务器
1.Mouse package com.yfs.javase; public class Mouse { private int index = 1; private boolean isLive = ...
随机推荐
- 以后坚持用java
1.不要贪多,现在专心学习java.读一些jvm的书. 2.研究lucene,hadoop.mahout,和日后用的自然语言分析lingpipe. 3.对于数据挖掘方向,专注与weka的学习,同时注意 ...
- Dynamices CRM Permission Issue (Security role UI to privilege mapping)'s solution
select * from privilege where privilegeid = 'a4736385-9763-4a64-a44b-cd5933edc631' Security role UI ...
- PC-启动慢的解决好方法!
你试试这样看行不行: 首先,打开“系统属性”(在我的电脑上点右键-属性,或者在控制面板里打开“系统”,快捷键win+pause break)点“高级”选项卡,在“启动和故障恢复”区里打开“设置”,去 ...
- CoreLocation框架的使用
CoreLocation框架使用 一.地图和定位的简介 1.应用场景 周边:找餐馆/找KTV/找电影院(团购APP) 导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达(地图APP) 2 ...
- 【腾讯Bugly干货分享】手游热更新方案xLua开源:Unity3D下Lua编程解决方案
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com/s/2bY7A6ihK9IMcA0bOFyB-Q 导语 xL ...
- Windows上常见的集中布尔类型的比较
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:Windows上常见的集中布尔类型的比较.
- Windows Server 2012 R2中的网络诊断命令
Get-NetAdapter Get-NetIPAddress Get-NetIPConfiguration(GIP) TNC :Pinging Servers and Trace Route tnc ...
- [GIF] Colors in GIF Loop Coder
In this lesson we cover the different methods for defining and animating colors in GIF Loop Coder. f ...
- [Javascript] The Array map method
One very common operation in programming is to iterate through an Array's contents, apply a function ...
- java09 队列Queue与Deque
队列Queue与Deque. Enumeration Hashtable与Hashtable子类Properties(资源配置文件) 引用类型(强.软.弱.虚)与WeakHashMap Identit ...