美文网首页
Java学习笔记 - 第014天

Java学习笔记 - 第014天

作者: 迷茫o | 来源:发表于2016-12-16 20:29 被阅读0次

每日要点

Clone

实现Cloneable接口
implements Cloneable
Cloneable接口是标识性接口

重写父类Object 的clone()方法

    @Override
    public Student clone() {
        Student temp = null;
        try {
            temp = (Student) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return temp;
    }

调用clone()

        Student s1 = new Student("爱迪生", 56);
        Student s2 = s1.clone();

Comparable

对象自身没有比较的能力 需要实现Comparable<>接口
implements Comparable<Student>
重写方法

    @Override
    public int compareTo(Student o) {
        // 比较this和other谁大谁小
        // return this.age - o.age;
        return this.name.compareTo(o.name);
    }

final

在变量前加final 变量变常量 不能赋值
在方法前加final 方法在子类 不能重写
在类前加final 类不能被继承

工具类

  • Arrays
    Arrays.sort(array);

  • 自己定义一个工具类
    写一个工具类的要点:

    • 所有的方法都应该是静态方法
    • 将构造器私有不允许调用构造器创建对象
    • 工具类一般不会被继承所以通常是final的
public final class CommonUtil {

    private CommonUtil() {
        throw new AssertionError();
    }
    
    public static int randomInt(int min, int max) {
        return (int) (Math.random() * (max - min + 1) + min);
    }
    
    public static Color randomColor() {
        return new Color(randomInt(0, 255), randomInt(0, 255), randomInt(0, 255));
    }
}

杂项

  • 标识性接口
    标识性接口 - 没有方法但是可以让类具备某种能力
public interface Creature {
}
  • instanceof
    instanceof 不能判断两个完全不相关的 需要有关联
    instanceof 只对对象的引用类型生效  枚举和八种基本数据类型 不可以

  • 完全限定名
    如果两个类的名字冲突了那么可以使用完全限定名加以区分
    所谓完全限定名(qualified class name)就是带包名的类名

        com.jack.future.Person person01 = 
                new com.jack.future.Person("小白");
  • 类里面加static的方法
    类里面加static的方法 是发给类的消息 可以直接类调用
    不加static的方法 是发给对象的消息 要先创建对象

昨天作业讲解

  • **1.题目:我想做燕子 只需简单思想 只求风中流浪
    我想做树 不长六腑五脏 不会寸断肝肠
    我做不成燕子 所以我 躲不过感情的墙
    我做不成树 因此也 撑不破伤心的网
    来生做燕子吧 随意找棵树休息翅膀 然后淡然飞向远方来
    生做树吧 枝头的燕子飞走时 不去留恋地张望 **

前世:

生物接口:

// 标识性接口 - 没有方法但是可以让类具备某种能力
public interface Creature {
}

燕子类:

public class Swallow implements Creature{
    
    public void thinkSimply() {
        System.out.println("只需简单思想");
    }
    
    public void wanderInWind() {
        System.out.println("只求风中流浪");
    }
}

树类:

public class Tree implements Creature{
    
    public void beHeartless() {
        System.out.println("不长六腑五脏 ");
        System.out.println("不会寸断肝肠");
    }
}

人类:

public class Person implements Creature{
    private String name;
    
    public Person(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
}

测试类:

        Creature person = new Person("小雨康桥");
        // instanceof 不能判断两个完全不相关的   需要有关联
        // instanceof 只对对象的引用类型生效 枚举和八种基本数据类型 不可以
        if (person instanceof Swallow) {
            ((Swallow) person).thinkSimply();
            ((Swallow) person).wanderInWind();
        }
        else {
            System.out.println("所以我飞不过感情的墙");
        }
        
        if (person instanceof Tree) {
            ((Tree) person).beHeartless();
        }
        else {
            System.out.println("因此也撑不破伤心的网");
        }

今生:

燕子接口:

// 接口之间可以相互继承
public interface Swallow extends Creature {

    public default void thingkSimply() {
        System.out.println("只需简单思想");
    }
    
    public default void wanderInWind() {
        System.out.println("只求风中流浪");
    }
}

树接口:

public interface Tree extends Creature {

    public default void beHearless() {
        System.out.println("不长六腑五脏 ");
        System.out.println("不会寸断肝肠");
    }
}

人类:

// 类可以实现多个接口 
public class Person implements Creature, Swallow, Tree {
    private String name;
    
    public Person(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }

    @Override
    public void beHearless() {
        System.out.println("当枝头燕子飞走时,不去留恋的张望");
    }

    @Override
    public void thingkSimply() {
        System.out.println("随意找棵树休息翅膀");
    }

    @Override
    public void wanderInWind() {
        System.out.println("然后淡然飞向远方");
    }
}

测试:

        Creature person = new Person("小雨康桥");
        if (person instanceof Swallow) {
            ((Swallow) person).thingkSimply();
            ((Swallow) person).wanderInWind();
        }
        if (person instanceof Tree) {
            ((Tree) person).beHearless();
        }

例子

  • 1.克隆和比较接口
    学生类:
public class Student extends Object implements Cloneable, Comparable<Student>{
    private String name;
    private int age;
    
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public Student clone() {
        Student temp = null;
        try {
            temp = (Student) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return temp;
    }
    
    @Override
    public String toString() {
        return name + ":" + age;
    }

    @Override
    public int compareTo(Student o) {
        // 比较this和other谁大谁小
        // return this.age - o.age;
        return this.name.compareTo(o.name);
    }
}

测试克隆:

        Student s1 = new Student("爱迪生", 56);
        Student s2 = s1.clone();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        s2.setName("王大锤");
        s2.setAge(15);
        System.out.println(s1);
        System.out.println(s2);

测试比较:

        //int[] array = {35, 23, 99, 18, 57, 66, 45, 30};
        //String[] array = {"grape", "hello", "abacus", "zoo", "killer"};
        //String[] array = {"丁春秋", "魏忠贤", "张三丰", "东风不败"};
        Student[] array = {
            new Student("Wang Dachui", 15),
            new Student("Lee Xiaolong", 28),
            new Student("Zhang Sanfeng", 35)
        };
        
        // 类里面加static的方法 是发给类的消息 可以直接类调用 
        // 不加static的方法 是发给对象的消息 要先创建对象
        Arrays.sort(array);
        for (Student x : array) {
            System.out.println(x + " ");
        }

作业

  • 1.黑杰克 21点

相关文章

网友评论

      本文标题:Java学习笔记 - 第014天

      本文链接:https://www.haomeiwen.com/subject/ubxomttx.html