Home>
The following program is an excerpt from "Clearly understand java practice" p131.
Output result (book description)
Equipment: Steel sword * Question
Clone with clone ()
Change the name of the sword of the hero who copied it
Shows the equipment of the hero of the copy source and copy destination
Copy source: Hinoki stick/Copy destination: Hinoki stick
Why is it "equipment: steel sword" even though the method is called with getName ()?
public class Main {
public static void main (String [] args) {
Hero h1 = new Hero ("Minato");
Sword s = new Sword ("Sword of Steel");
H1.setSword (s);
System.out.println ("Equipment:" + h1.getSword (). GetName ());
System.out.println ("duplicate with clone ()");
Hero h2 = h1.clone ();/* Duplicate here * /
System.out.println ("Rename the original hero's sword");
h1.getSword (). setName ("Hinoki cypress stick");
System.out.println ("Shows source and destination hero equipment");
System.out.println ("copy source:" + h1.getSword (). getName () +
"/ Copy to:" + h2.getSword (). getName ());
}
}
public class Hero implements Cloneable {
String name;// name
int hp;// HP
Sword sword;// Equipped weapon
public Hero clone () {
Hero result = new Hero (name);
result.name = this.name;
result.hp = this.hp;
result.sword = this.sword;
return result;
}
public Hero (String name) {
this.name = name;
}
public Sword getSword () {
return sword;
}
public void setSword (Sword sword) {
this.sword = sword;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
}
public class Sword {
private String name;
public Sword (String name) {
this.name = name;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
}
-
Answer # 1
-
Answer # 2
I'm not sure from the point of view of the question, but do you set it in advance?
Sword s = new Sword ("Sword of Steel"); H1.setSword (s); System.out.println ("Equipment:" + h1.getSword (). GetName ());
Related articles
- [java 8] about the output of arraylist
- java - questions about a simple nested loop statement
- java - result output program of equations (addition, subtraction level)
- java - i want to output the result of winning or losing of rock-paper-scissors
- java - about localhost
- javascript: about output of multidimensional array
- java - about the two arguments of onclick
- python 3x - i want to output the python3 ping result to csv
- java - about the stringutilsisempty () method
- about jasperreports form output in eclipse
- beginners about the development of java spring tool suite
- java - about js that displays images randomly
- javascript - i want to output the profit calculation result using mathfloor
- javascript - about select output of shopify product page form
- java - to output tomcat gc log on windows
- ruby - questions about the contents of the aws reference site
- about java jdk download
- java - about nullpointerexception
- java - the result of polynomial multiplication is strange
Trends
Minato is for h1.getNmae ().
Because there is
Is
Will be the same as.