Java编程

java尚硅谷基础笔记

public class Person {protected name;protected age;}//老师继承了人,所以有了姓名和年龄属性public class Teacher extends Person {}如果这个时候,老师具备了教学的能力?那么Java只需要再Teacher中实现teachable这个接口就可以:

java尚硅谷基础笔记

编程题1.import java.util.*;public class Test02 {public static void main(String[] args) {HashSet hashSet = new HashSet();Person p1 = new Person(“Jack”,25);Person p2 = new Person(“Rose”,23);Person p3 = new Person(“Jack”,27);hashSet.add(p1);hashSet.add(p2);hashSet.add(p3);for(Object obj:hashSet){ Person p=(Person)obj; System.out.println(p.name+”:”+p.age);}}}class Person{String name;int age;public Person(String name, int age) {super();this.name = name;this.age = age;}public int hashCode() { return name.hashCode();}public boolean equals(Object obj) {if (this == obj) return true;if (obj == null)return false;Person p = (Person) obj;return p.name.equals(this.name);}。

public class Person {private String name;private int age;public Person(String name,int age){this.name = name;this.age = age;}public String getName(){return this.name;}public int getAge(){return this.age;}@Overridepublic String toString(){return “(Person:”+name+”,”+age+”)”;}public boolean equals(Object o){if (this == o){return true;}if (o instanceof Person){Person p = (Person) o;return Objects.equals(p.name,this.name) && p.age == this.age;}return false;}}Main.java。

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注