面向對象思想
經典案例1
package Day12_07_01;
import java.util.Scanner;
public class Text2
{
private static final double AISLE_UNIT_PRICE = 38.5;
private static final double F_UNIT_PRICE = 15.5;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("請輸入圓的半徑:");
double r=input.nextDouble();
Circle small =new Circle(r);
Circle big=new Circle(r+3);
System.out.printf("圍墻的造價: ¥%.2f\n",big.perimeter()*F_UNIT_PRICE);
System.out.printf("過道的造價: ¥%.2f\n",(big.area()-small.area())*AISLE_UNIT_PRICE);
input.close();
}
}
package Day12_07_01;
public class Circle
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public double perimeter(){
return 2*Math.PI*radius;
}
public double area(){
return Math.PI*radius*radius;
}
}
案例2
package Day12_07_01;
public class Ultraman
{
String name;
int hp;
int mp;
public int getHp()
{
return hp;
}
public void setHp(int hp)
{
this.hp = hp > 0 ? hp : 0;
}
public int getMp()
{
return mp;
}
public void setMp(int mp)
{
this.mp = mp;
}
// 創(chuàng)建構造器
public Ultraman(String name, int hp, int mp)
{
this.name = name;
this.hp = hp;
this.mp = mp;
}
public int attact(Monster monster)
{
int injury = (int) (Math.random() * 11 +20);
monster.setHp(monster.getHp() - injury);
return injury;
}
public void hugeAttact(Monster monster)
{
if (mp >= 40)
{
int injory = monster.getHp() / 4 * 3 > 50 ? monster.getHp() / 4 * 3 : 50;
monster.setHp(monster.getHp() - injory);
mp -= 40;
}
}
public void magicalAttact(Monster[] mArray)
{
if (mp >= 15)
{
for (Monster monster : mArray)
{
int injury = (int) (Math.random() * 11 + 20);
monster.setHp(monster.getHp() - injury);
}
mp -= 15;
}
}
public String info()
{ // %s放入一個字符串
return String.format("%s奧特曼-生命值:%d,魔法值:%d", name, hp, mp);
}
}
package Day12_07_01;
public class Monster
{
String name;
int hp;
public int getHp()
{
return hp;
}
public void setHp(int hp)
{
this.hp = hp < 0 ? 0 : hp;
}
public Monster(String name, int hp)
{
this.name = name;
this.hp = hp;
}
public int attact(Ultraman ultraman)
{
int injury = (int) (Math.random() * 11 + 10);
ultraman.setHp(ultraman.getHp() - injury);
return injury;
}
public String info()
{
return String.format("%s小怪獸-生命值:%d", name, hp);
}
}
package Day12_07_01;
//在main方法中可以直接調用的方法必須要加上static
public class Text3
{
//判斷奧特曼攻擊的攻擊的哪支小怪獸
public static Monster selectOne(Monster[] mArray)
{
Monster temp = null;
do
{
int randomIndex = (int) (Math.random() * mArray.length);
temp = mArray[randomIndex];
} while (temp.getHp() == 0);
return temp;
}
//顯示所有的小怪獸的信息
public static void shouMonsterInfo(Monster[] mArray)
{
for (Monster m : mArray)
{
System.out.println(m.info());
}
}
//判斷小怪獸是否死光了
public static boolean isAllDead(Monster[] mArray)
{
for (Monster monster : mArray)
{
if (monster.getHp() > 0)
{
return false;
}
}
return true;
}
public static void monsterIsAttact(Ultraman ultraman, Monster monster)
{
if (monster.getHp() > 0)
{
monster.attact(ultraman);
}
}
public static void main(String[] args)
{
Ultraman ultraman = new Ultraman("舒玲", 200, 150);
System.out.println(ultraman.info());
Monster monster1 = new Monster("王煥琪", 100);
Monster monster2 = new Monster("王琪", 100);
Monster monster3 = new Monster("王煥", 100);
Monster monster4 = new Monster("煥琪", 100);
Monster[] mArray ={ monster1, monster2, monster3, monster4 };
shouMonsterInfo(mArray);
int round = 1;
do
{
System.out.println("======第" + round + "回合=======");
int random = (int) (Math.random() * 10 + 1);
Monster monster =selectOne(mArray);
if (random <= 7)
{
System.out.println("奧特曼使用了普通攻擊蹄殃!");
ultraman.attact(monster);
monsterIsAttact(ultraman, monster);
} else if (random <= 9)
{
if (ultraman.getMp() > 15)
{
System.out.println("奧特曼使用了魔法攻擊!");
ultraman.magicalAttact(mArray);
for (Monster m : mArray)
{
monsterIsAttact(ultraman, m);
}
} else
{
System.out.println("奧特曼使用了普通攻擊匿醒!");
ultraman.attact(monster);
monsterIsAttact(ultraman, monster);
}
} else
{
if (ultraman.getMp() > 50)
{
System.out.println("奧特曼使用了必殺技拱绑!");
ultraman.hugeAttact(monster);
} else
{
System.out.println("奧特曼使用了普通攻擊耀石!");
ultraman.attact(monster);
monsterIsAttact(ultraman, monster);
}
}
System.out.println(ultraman.info());
shouMonsterInfo(mArray);
round++;
} while (ultraman.getHp() > 0 &&!isAllDead(mArray));
if (ultraman.getHp() > 0)
{
System.out.println("奧特曼勝利力崇!");
} else
{
System.out.println("小怪獸勝利!");
}
}
//小怪獸是否攻擊奧特曼
}
注釋文檔的使用
在Line類中去調用Point類
package Day12_07_02;
/**
* 平面上的線條
* @author YY
*@since 0.1
*/
public class Line
{
private Point start;
private Point end;
/**
*構造器
* @param start 線段的起點
* @param end 線段的終點
*/
public Line(Point start, Point end)
{
this.start = start;
this.end = end;
}
/**
* 獲取線斷的長度
* @return 線段的長度
*/
public double length(){
return start.distance(end);
}
}
package Day12_07_02;
/**
* 平面上的點
* @author YY
* @since 0.1
*
*/
public class Point
{
//private類型不用寫文檔注釋
private double x;
private double y;
/**
* 構造器
* @param x 橫坐標
* @param y 縱坐標
*/
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public double getX()
{
return x;
}
public void setX(double x)
{
this.x = x;
}
public double getY()
{
return y;
}
public void setY(double y)
{
this.y = y;
}
public void show()
{
System.out.printf("橫坐標:%.2f縱坐標:%.2f", x, y);
}
/**
* 移動到
* @param newX 新的橫坐標
* @param newY 新的縱坐標
*/
public void moveTo(double newX, double newY)
{
x = newX;
y = newY;
}
public void moveBy(double dx, double dy)
{
x += dx;
y += dy;
}
/**
* 計算到另一個點的距離
* @param other 另一個點
* @return 兩個點的距離
*/
public double distance(Point other)
{
double dx = this.x - other.x;
double dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
public String toString()
{
return "(" + x + "," + y + ")";
}
}
package Day12_07_02;
public class Text4
{
public static void main(String[] args)
{
Point p1 = new Point(5, 5);
System.out.println(p1.toString());
Point p2 = new Point(2, 4);
System.out.println(p2.toString());
System.out.println(p1.distance(p2));
p1.moveBy(9, 9);
p2.moveTo(10, 10);
System.out.println(p1.distance(p2));
Line line=new Line(p1, p2);
System.out.println(line.length());
}
}