前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >每日一题(17)

每日一题(17)

作者头像
KEN DO EVERTHING
发布2019-01-17 16:46:20
3130
发布2019-01-17 16:46:20
举报
文章被收录于专栏:KEN DO EVERTHINGKEN DO EVERTHING

上题回顾与解析

代码语言:javascript
复制
public class Point {
    protected  int x,y;
    private  String name;

    Point(int x,int y){
        this.x = x;
        this.y = y;
        //3.此处调用子类的makeName方法
        //因为该方法被子类方法覆盖了
        this.name = makeName();
    }

    protected  String makeName(){
        return "["+ x + "," + y +"]";
    }

    @Override
    public String toString(){
        return name;
    }

}

class ColorPoint extends Point{
     private String color;

     ColorPoint(int x, int y, String color){
         //2.调用父类的构造方法
         super(x,y);
         this.color = color;
     }

     @Override
     protected String makeName(){
         //4.在ColorPoint构造函数未执行完时,color未null
         //所以返回[4,2]:null
         return super.makeName() + ":" + color;
     }

     public static void main(String[] args){
         //1.调用子类的构造方法
         System.out.println(new ColorPoint(4, 2, "blue"));
     }
}

看完题目有点迷糊?那看来你并没有掌握好类的初始化顺序 这种题目理清楚执行顺序就好办。执行顺序及解析为题上面程序中的1234

每日一题

下面程序输出什么?

代码语言:javascript
复制
public class Name {
    private final  String first, last;

    public Name(String first, String last){
        this.first = first;
        this.last = last;
    }

    @Override
    public  boolean equals(Object o){
        if(o instanceof Name){
            return  false;
        }
        Name n = (Name)o;
        return n.first.equals(first) && n.last.equals(last);
    }

    public static  void main(String[] args){
        Set<Name> s = new HashSet<>();
        s.add(new Name("Mickey", "Mouse"));
        System.out.println(
                s.contains(new Name("Mickey", "Mouse")));
    }
}

坚持日更:30天

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-11-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 java从心 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档