面向对象(上)-构造器练习:三角形
package top.qaqaq.java.P229;
/*
 * 编写两个类,TriAngle和TriAngleTest,其中TriAngle类中声明私有的底边长base和高height,同时声明公共方法访问私有变量。
 * 此外,提供类必要的构造器。另一个类中使用这些公共方法,计算三角形的面积。
 * 
 * 
 * 
 * 
 */
public class TirAngle {	//angle:角	angel:天使
	
	private double base;//底边长
	private double height;//高
	private double area;
	
	public TirAngle() {
		
	}
	
	public TirAngle(double b,double h) {
		base = b;
		height = h;
		System.out.println(base * height / 2);
		
	}
	
	public void setBase(double b) {
		base = b;
	}
	
	public double getBase() {
		return base;
	}
	
	public void setHeight(double h) {
		height = h;
	}
	
	public double getHeight() {
		return height;
	}
	

}
package top.qaqaq.java.P229;

public class TriAngleTest {
	
	public static void main(String[] args) {
		
		TirAngle t1 = new TirAngle();
		
		t1.setBase(2.0);
		t1.setHeight(2.4);
//		t1.base = 2.5;//The field TirAngle.base is not visible
//		t1.height = 4.3;
		
		System.out.println("base:" + t1.getBase() + ",height:" + t1.getHeight());

		TirAngle t2 = new TirAngle();
		System.out.println("base:" + t2.getBase() + ",height:" + t2.getHeight());
	
	}

}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇