Java - 面向对象练习 - market
Posted 幽暗森林之猪大屁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java - 面向对象练习 - market相关的知识,希望对你有一定的参考价值。
Market
package market;
public class Market {
private String marname;
private Product[] productarr;
public Product[] getProductarr() {
return productarr;
}
public String getMarname() {
return marname;
}
public void setMarname(String marname) {
this.marname = marname;
}
public void setProductarr(Product[] productarr) {
this.productarr = productarr;
}
Product sell(String name){
for (int i = 0; i < productarr.length; i++) {
if (productarr.getProname() == name){
return productarr;
}
}
return null;
}
}
Person
package market;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Product shopping(Market market,String name){
return market.sell(name);
}
}
Product
package market;
public class Product {
private String proname;
public void setProname(String proname) {
this.proname = proname;
}
public String getProname() {
return proname;
}
}
Shopping
package market;
public class Shopping {
public static void main(String[] args) {
Product p1 = new Product();
Product p2 = new Product();
Product p3 = new Product();
Product p4 = new Product();
Product p5 = new Product();
// 创建商品类
p1.setProname("1");
p2.setProname("2");
p3.setProname("3");
p4.setProname("4");
p5.setProname("5");
// 给商品类赋名
Market m = new Market();
m.setMarname("k商店");
m.setProductarr(new Product[]{p1,p2,p3,p4,p5});
// 创建商店类、赋名、添加商品
Person p = new Person();
p.setName("alex");
Product result = p.shopping(m,"20");
if (result != null){
System.out.println(p.getName()+"在"+m.getMarname()+"买到了"+result.getProname());
} else {
System.out.println(p.getName()+"在"+m.getMarname()+"白跑了一趟");
}
}
}
更多技术资讯可关注:itheimaGZ获取
以上是关于Java - 面向对象练习 - market的主要内容,如果未能解决你的问题,请参考以下文章