斯坦福 IT

月盈亏

月月盈
最大赞力
0.00
当前赞力
100.00%
[/QUOTE=
public void A(){
try{
B();
C();
} catch(IOException e){
//handle exception there
}
}

private void B() throws IOException{
...
}

private void C() throws IOException{
...
}[/QUOTE]

你擅长用实例解释抽象的概念,太适合我了。你是我老师多好。
 
最大赞力
0.00
当前赞力
100.00%
Just downloaded NetBeans. Great tools, much better than notepad++!

Prompted to import the following class libs automatically.
import java.util.ArrayList;
import java.util.List;

Wooden's codes get compiled correctly and got the following outputs:
upload_2018-4-13_10-25-25.png

练脑子的第一步:下载,安装IDE, 熟悉界面,编译和运行程序

谢谢宝哥的NetBeans.
 
最大赞力
0.00
当前赞力
100.00%
public class Planet {

public Planet(String name) {
super();
this.name = name;
}

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

public class SpaceShip {

private String shipName;
private Planet currentPlannt;

public SpaceShip(String shipName) {
super();
this.shipName = shipName;
}
public String getShipName() {
return shipName;
}
public Planet getCurrentPlannt() {
return currentPlannt;
}
public void setShipName(String shipName) {
this.shipName = shipName;
}
public void setCurrentPlannt(Planet currentPlannt) {
this.currentPlannt = currentPlannt;
}

public void moveTo(Planet p){
System.out.println("SpaceShip "+this.getShipName()+" move from "+(this.currentPlannt==null?"Unknown space ":this.currentPlannt.getName())+" To "+p.getName());
this.setCurrentPlannt(p);
}
}

public class SpaceGame {

List<Planet> planets = new ArrayList<Planet>();
SpaceShip shipA = new SpaceShip("A");
SpaceShip shipB = new SpaceShip("B");

public static void main(String[] args){

SpaceGame game = new SpaceGame();
Planet p1 = new Planet("Earth");
Planet p2 = new Planet("Mars");
Planet p3 = new Planet("Jupitor");
Planet p4 = new Planet("Venus");
Planet p5 = new Planet("Saturn");
game.planets.add(p1);
game.planets.add(p2);
game.planets.add(p3);
game.planets.add(p4);
game.planets.add(p5);
game.shipA.moveTo(game.findPlanetByName("Earth"));
game.shipB.moveTo(game.findPlanetByName("Saturn"));
game.shipA.moveTo(game.findPlanetByName("Mars"));
game.shipB.moveTo(game.findPlanetByName("Jupitor"));


}

public List<Planet> getPlanets() {
return planets;
}

public SpaceShip getShipA() {
return shipA;
}

public SpaceShip getShipB() {
return shipB;
}

public void setPlanets(List<Planet> plannets) {
this.planets = plannets;
}

public Planet findPlanetByName(String name){
for (Planet p: planets){
if (p.getName().equals(name))
return p;
}
return null;
}

public void addPlanet(Planet p){
this.planets.add(p);
}

public void setShipA(SpaceShip shipA) {
this.shipA = shipA;
}

public void setShipB(SpaceShip shipB) {
this.shipB = shipB;
}



}

I saw you create public methods for every private attributes, such as you created "public method Planet.getName" for "private attribute Planet.name". Why not just make the attribute itself public? 用中文讲,何此一举啊?

问题可能很幼稚,宝哥和小清风可以帮回答一下吗?
 
最大赞力
0.01
当前赞力
100.00%
I saw you create public methods for every private attributes, such as you created "public method Planet.getName" for "private attribute Planet.name". Why not just make the attribute itself public? 用中文讲,何此一举啊?

问题可能很幼稚,宝哥和小清风可以帮回答一下吗?
You can control your class better in this way. For example, you pay money to a vendor, but the vendor cannot take from your wallet, you may also check if there is still enough money before you pay, you can say I can pay you only half now.
 

gongbao

宇宙最最知名园友
最大赞力
0.00
当前赞力
100.00%
I saw you create public methods for every private attributes, such as you created "public method Planet.getName" for "private attribute Planet.name". Why not just make the attribute itself public? 用中文讲,何此一举啊?

问题可能很幼稚,宝哥和小清风可以帮回答一下吗?
你搜索一下public protected private这些,跟面向对象类的安全等级有关的,总之能private就尽量私有
 

gongbao

宇宙最最知名园友
最大赞力
0.00
当前赞力
100.00%
Just downloaded NetBeans. Great tools, much better than notepad++!

Prompted to import the following class libs automatically.
import java.util.ArrayList;
import java.util.List;

Wooden's codes get compiled correctly and got the following outputs:
浏览附件492610

练脑子的第一步:下载,安装IDE, 熟悉界面,编译和运行程序

谢谢宝哥的NetBeans.
哈,你是比清风还嫩的新手啊
 
最大赞力
0.00
当前赞力
100.00%
How to set static attribute in a class?

http://www.chegg.com/homework-help/...ode-spaceship-planets-game-loop-mov-q28227474
3) The setPlanets method
This method sets the planets attribute. This method takes an ArrayList of planets as input.
Remember that ArrayList is a mutable reference type and as such you should make a copy of the
input ArrayList
when initializing the attribute. That is, you cannot just set the attribute to be
the parameter, as they will both point to the same data in memory.
After the list is loaded, loop through the ArrayList and print out the details of every planet. Hint:
Use the toString method of the Planet class

Questions:
1. How to make a copy of the ArrayList.
2. How refer the static attribute in the class?

I am researching for the answers. Help appreciated.

Thanks.
 
最大赞力
0.00
当前赞力
100.00%
How to set static attribute in a class?

http://www.chegg.com/homework-help/...ode-spaceship-planets-game-loop-mov-q28227474
3) The setPlanets method
This method sets the planets attribute. This method takes an ArrayList of planets as input.
Remember that ArrayList is a mutable reference type and as such you should make a copy of the
input ArrayList
when initializing the attribute. That is, you cannot just set the attribute to be
the parameter, as they will both point to the same data in memory.
After the list is loaded, loop through the ArrayList and print out the details of every planet. Hint:
Use the toString method of the Planet class

Questions:
1. How to make a copy of the ArrayList.
2. How refer the static attribute in the class?

I am researching for the answers. Help appreciated.

Thanks.

这是谁的代码?
upload_2018-4-13_17-30-5.png
 

Similar threads

家园推荐黄页

家园币系统数据

家园币池子报价
家园币最新成交价
家园币总发行量
加元现金总量
家园币总成交量
家园币总成交价值

池子家园币总量
池子加元现金总量
池子币总量
1池子币现价
池子家园币总手续费
池子加元总手续费
入池家园币年化收益率
入池加元年化收益率

微比特币最新报价
毫以太币最新报价
微比特币总量
毫以太币总量
家园币储备总净值
家园币比特币储备
家园币以太币储备
比特币的加元报价
以太币的加元报价
USDT的加元报价

交易币种/月度交易量
家园币
加元交易对(比特币等)
USDT交易对(比特币等)
顶部