1、Shap类
package com.yfs.javase; public class Shape /*extends Object */{ //默认继承object object方法全部继承 //计算面积方法 public double getArea() { System.out.println("计算面积"); return 0; } }
2.Circle类
package com.yfs.javase; public class Circle extends Shape { private double r; public Circle(double r) { this.r = r; System.out.println("创建圆形面积"); } public double getArea() {//覆盖父类的方法 System.out.println("计算圆形面积..."); return 3.14 * r * r; } }
3.Rangton类
package com.yfs.javase; public class Rangton extends Shape { private double width; private double length; public Rangton(double width, double length) { this.width = width; this.length = length; System.out.println("创建矩形面积"); } public double getArea() { System.out.println("计算矩形面积..."); return width * length; } }
4.Trantangel类
package com.yfs.javase; public class Trantangle extends Shape { private double height; private double width; public Trantangle(double height, double width) { this.height = height; this.width = width; System.out.println("创建三角形面积"); } public double getArea() { System.out.println("计算三角形面积..."); return 1.0 / 2 * width * height; } }
5.Shap 测试
package com.yfs.javase; import java.util.Date; import java.util.Random; import java.util.Scanner; public class Demo1 { /** * @param args */ public static void main(String[] args) { Shape shape = new Shape(); shape.getClass(); Object obj = new Object();// 所有对象的基类 // 方法 boolean isTrue = obj.equals(shape); System.out.println(isTrue); obj.getClass(); obj.hashCode(); obj.toString(); //obj.notify(); obj = shape; obj = new Random(); obj = new Scanner(System.in); Circle c = new Circle(2); obj = c; shape = c; obj = shape; //继承object c.getClass(); method(shape); //method(new Date()); } public static void method(Object obj) { //obj.getArea(); Shape s = (Shape)obj; s.getArea(); } }
6.Person 类(自定义比较)
package com.yfs.javase; public class Person { private String name; private char sex; private int age; public Person() { } public Person(String name, char sex, int age) { this.name = name; this.sex = sex; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void sayHello() { System.out.println(name + " Hello ...."); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + sex; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (sex != other.sex) return false; return true; } // @Override // public int hashCode() { // return 123; // } // // @Override //自定义类 覆盖equals方法 自己定义对象比较规则 // public boolean equals(Object obj) { // Person other = (Person)obj; // return this.name.equals(other.name) && sex == other.sex && age == other.age; // } }
7.Person测试
package com.yfs.javase; public class Demo2 { /** * @param args */ public static void main(String[] args) { Person p1 = new Person(); System.out.println("p1.hashCode() : " + p1.hashCode()); Person p2 = new Person();//hashCode对象的散列码 System.out.println("p2.hashCode() : " + p2.hashCode()); Person p3 = new Person(); System.out.println("p3.hashCode() : " + p3.hashCode()); p1.sayHello(); p2.sayHello(); p3.sayHello(); System.out.println("p1 == p2 ? " + (p1 == p2)); } }
8.Person类
package com.yfs.javase; public class Demo3 { /** * @param args */ public static void main(String[] args) { Person p1 = new Person("张三",‘男‘,20); Person p2 = new Person("张三",‘男‘,20); System.out.println("p1 == p2 : " + (p1 == p2)); //比较对象中属性只是否相等 System.out.println(p1.getAge() == p2.getAge()); System.out.println("== " + (p1.getName() == p2.getName())); System.out.println("equals : " + p1.getName().equals(p2.getName())); System.out.println("p1.equals(p2) : " + p1.equals(p2)); System.out.println("======================"); System.out.println("p1.hashcode :" + p1.hashCode()); // String s1 = new String("abc"); // String s2 = new String("abc"); // System.out.println("s1 == s2 : " + (s1 == s2)); // System.out.println("s1.equals(s2) : " + s1.equals(s2));//String 类覆盖equals方法 Object obj = p1; System.out.println(obj.getClass().getName()); // } }
时间: 2024-11-19 22:34:18