Java类应用举例

来自CloudWiki
跳转至: 导航搜索

几何计算

球体类

设计一个球类,成员变量有半径,能调用它的方法求体积和表面积。

成员变量

半径:

private double r;

构造方法

 
public Ball() {//构造方法1
    	this.r =3;
    }
    public Ball(double r) {//构造方法2
    	this.r=r;
    }

成员方法

半径r的读写方法:

public double getR() {//r的读方法
    	return this.r;
    }
    public void setR(double r) {//r的写方法
    	this.r = r;
    }

求体积的方法:

   public double volume() {
    	final double pi=3.14;
    	double v=4*pi* this.r *this.r * this.r /3;
    	return v;
    }

求表面积的方法:

   public double surfaceArea(){
    	final double pi =3.14;
    	double s = 4*pi * this.r * this.r;
    	return s;
    }

完整代码

package task7;

public class Ball {
    private double r;
    public Ball() {//构造方法1
    	this.r =3;
    }
    public Ball(double r) {//构造方法2
    	this.r=r;
    }
    public double getR() {//r的读方法
    	return this.r;
    }
    public void setR(double r) {//r的写方法
    	this.r = r;
    }
    public double volume() {
    	final double pi=3.14;
    	double v=4*pi* this.r *this.r * this.r /3;
    	return v;
    }
    public double surfaceArea(){
    	final double pi =3.14;
    	double s = 4*pi * this.r * this.r;
    	return s;
    }
    public String toString() {
    	return "球体半径为:"+this.r+";体积为:"+this.volume()+";表面积为: "+this.surfaceArea();
    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Ball b1 = new Ball();
		b1.setR(4.5);
		System.out.println("球体半径 r="+b1.getR());
		Ball b2 = new Ball(2);
		System.out.println("球体体积v="+b2.volume());
		System.out.println("球体表面积s="+b2.surfaceArea());
		System.out.println(b2.toString());
		
		
		
		

	}

}

练习:设计长方体类

设计长方体类,初始化长、宽、高,求它的表面积和体积。

游戏开发

角色类

package task7;

public class Hero {
    private String name;
    private int age;
    private double healthPoint;
    private String[]  treasure;
    
    public Hero() {
    	this.name="xiaolan";
    	this.age=18;
    	this.healthPoint=100;
    	this.treasure =  new String[3];
    	treasure[0]="倚天剑";treasure[1]="屠龙刀";treasure[2]="九步穿肠散";
    }
    public String toString() {
    	return "角色名:"+this.name + "年龄: "+this.age
    			+"健康值:"+this.healthPoint+"武器库: "+this.treasure.length;
    }
    public void SearchArms() {//搜索全部武器
    	System.out.println("武器库介绍:");
    	for(int i=0;i<this.treasure.length;i++) {
    		System.out.println("     "+treasure[i]);
    	}
    	System.out.println();
    }
    public void SearchArms(String s) {//搜索特定武器
    	System.out.println("武器库中搜索:"+s);
    	int count=0;
    	for(int i=0;i<this.treasure.length;i++) {
    		if(treasure[i].equals(s)) {
    			System.out.println("     找到"+s+" 1件\n");
    			count++;
    			break;
    		}
    		
    	}
    	if(count==0) {
    		System.out.println("     没有找到该武器\n");
    	}
    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Hero h= new Hero();
		System.out.println(h.toString());
		h.SearchArms();
		h.SearchArms("九步穿肠散");
		h.SearchArms("迷魂汤");
		
        
	}

}

角色名:xiaolan年龄: 18健康值:100.0武器库: 3 武器库介绍: 倚天剑 屠龙刀 九步穿肠散 武器库中搜索:九步穿肠散 找到九步穿肠散 1件 武器库中搜索:迷魂汤 没有找到该武器