?总结:对象一对一。
package COM.WWW;
import java.security.DigestException;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class HelloIDEA {
public static void main(String[] args) {
yaSuo yasuo = new yaSuo("亚索", 6300, 9);
sword sword = new sword("无鞘之刃", 500, 90);
sword.setYaSuo(yasuo);
yasuo.setsword(sword);
String x = yasuo.getName();
int skill = yasuo.getSkill();
int price = yasuo.getPrice();
System.out.println("我是" + x + "价格" + price + "刀的名字是" + yasuo.getSword().getName() + "刀的价格" + yasuo.getSword().getPrice()
+ "我的等级是" + yasuo.getSkill() + "刀的等级是" + yasuo.getSword().getSkill());
}
}
class yaSuo{
private String name;
private int price;
private int skill;
private sword sword;
public void setsword(sword sword){
this.sword=sword;
}
public sword getSword() {
return sword;
}
public yaSuo(){}
public yaSuo(String name,int price,int skill){
this.name=name;
this.price=price;
this.skill=skill;
}
public void setName(String name){//获得名字
this.name=name;
}
public String getName(){//输出名字
return name;
}
public void setPrice(int price) {//获得价格
this.price=price;
}
public int getPrice() {//输出价格
return price;
}
public void getSkill(int skill) {//获得等级
this.skill = skill;
}
public int getSkill() {//输出等级
return skill;
}
}
class sword{
private String name;
private int price;
private int skill;
private sword sword;
private yaSuo yaSuo;
public void setYaSuo(yaSuo yaSuo) {
this.yaSuo = yaSuo;
}
public yaSuo getYaSuo() {
return yaSuo;
}
public sword(){}
public sword(String name,int price,int skill){
this.name=name;
this.price=price;
this.skill=skill;
}
public void setName(String name) {//输入名字
this.name = name;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getSkill() {//输出等级
return skill;
}
public void setSkill(int skill) {//输入等级
this.skill = skill;
}
public int getprice(){//输出价格
return price;
}
public void setPrice(int price) {//输出价格
this.price = price;
}
}
|