| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 598 人关注过本帖, 1 人收藏
标题:求解继承问题、、
只看楼主 加入收藏
申请幸福
Rank: 1
等 级:新手上路
帖 子:29
专家分:5
注 册:2013-10-25
结帖率:90.91%
收藏(1)
已结贴  问题点数:16 回复次数:5 
求解继承问题、、
程序代码:
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sca = new Scanner(System.in);
        System.out.println("已买到商品清单:");
        while (sca.hasNext()) {
            int n = sca.nextInt();
            ShopCar ps = new ShopCar(n + 1);
            for (int i = 1; i <= n; i++) {
                String sh = sca.next();
                char c = sh.charAt(0);

                if (c == 'B') {
                    ps.add(new Book(sca.next(), sca.nextFloat(), sca.nextInt(),
                            sca.next(), sca.next()));
                    ps.print();
                }
                if (c == 'C') {
                    ps.add(new Cloths(sca.next(), sca.nextFloat(), sca
                            .nextInt(), sca.next(), sca.next()));
                    ps.print();
                }
            }
        }

    }
}

abstract class Goods {
    private String name;
    private float price;
    private int count;

    Goods(String name, float price, int count) {
        this.setName(name);
        this.setPrice(price);
        this.setCount(count);
    }

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

    public String getName() {
        return this.name;
    }

    public void setPrice(float p) {
        this.price = p;
    }

    public float getPrice() {
        return this.price;
    }

    public void setCount(int n) {
        this.count = n;
    }

    public int getCount() {
        return this.count;
    }

    public abstract void getInfo();
}

class Book extends Goods {
    private String publish;
    private String author;

    Book(String name, float price, int count, String au, String pu) {
        super(name, price, count);
        this.setAuthor(au);
        this.setPublish(pu);
    }

    public void setAuthor(String n) {
        this.author = n;
    }

    public String getAuthor() {
        return this.author;
    }

    public void setPublish(String n) {
        this.publish = n;
    }

    public String getPublish() {
        return this.publish;
    }

    public void getInfo() {
        System.out.println("书名:" + super.getName() + ",单价:" + super.getPrice()
                + "f,数量:" + super.getCount());
    }

}

class Cloths extends Goods {
    private String title;
    private String style;

    Cloths(String name, float price, int count, String au, String pu) {
        super(name, price, count);
        this.setTitle(au);
        this.setStyle(pu);
    }

    public void setTitle(String n) {
        this.title = n;
    }

    public String getTitle() {
        return this.title;
    }

    public void setStyle(String n) {
        this.style = n;
    }

    public String getStyle() {
        return this.style;
    }

    public void getInfo() {
        System.out.println("品名:" + super.getName() + ",单价:" + super.getPrice()
                + "f,数量:" + super.getCount());
    }

}

class ShopCar {
    private Goods[] goods;
    private int foot;

    public ShopCar(String n, float price, int count) {

    }

    public ShopCar(int len) {
        if (len > 0) {
            this.goods = new Goods[len];
        } else {
            this.goods = new Goods[1];
        }
    }

    public boolean add(Goods goods) {
        if (this.foot < this.goods.length) {
            this.goods[this.foot] = goods;
            return true;
        } else {
            return false;
        }
    }

    public void print() {

        for (int i = 0; i < goods.length; i++) {
            if (goods[i] != null) {
                goods[i].getInfo();// 这个调用的是子类中的信息吗??
            }
        }
    }
}
搜索更多相关主题的帖子: color 
2014-04-02 17:02
jiewen
Rank: 2
等 级:论坛游民
威 望:1
帖 子:5
专家分:17
注 册:2014-4-1
收藏
得分:6 
回复 楼主 申请幸福
是的
2014-04-03 22:40
walqwanba
Rank: 1
等 级:新手上路
帖 子:2
专家分:6
注 册:2014-4-4
收藏
得分:6 
是的,你复写了父类的方法,所以调用的是子类的。但是我 总觉得哪里有问题,你说是调用哪个子类的getInfo()方法了??
2014-04-04 16:07
申请幸福
Rank: 1
等 级:新手上路
帖 子:29
专家分:5
注 册:2013-10-25
收藏
得分:0 
是的,你复写了父类的方法,所以调用的是子类的。但是我 总觉得哪里有问题,你说是调用哪个子类的getInfo()方法了??

就是说我复写了父类的抽象方法,当我调用父类的getInfo()时,它会输出子类里的吗??
2014-04-05 14:41
salestina
Rank: 2
等 级:论坛游民
威 望:1
帖 子:22
专家分:11
注 册:2014-4-3
收藏
得分:6 
子类重写了父类的方法后,子类的实例化对象在调用方法时,优先调用子类重写的方法
如果是构造方法重写的话
1.子类没有通过super调用父类的有参构造方法,也没有通过this调用自身其他的构造方法,那么子类实例化对象时会默认调用父类无参构造方法
2.子类构造方法通过super调用了父类的有参构造方法,那将执行父类的有参构造方法,不会调用父类的无参构造方法
3.如果子类的构造方法中通过this调用了自身其他的构造方法,在相应调用的构造方法中执行以上两种
这样说你能明白了吗???
2014-04-06 23:02
申请幸福
Rank: 1
等 级:新手上路
帖 子:29
专家分:5
注 册:2013-10-25
收藏
得分:0 
嗯、、、懂了、、谢谢。。
2014-04-07 11:01
快速回复:求解继承问题、、
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016865 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved