java新手筆記18 類比較

來源:互聯網
上載者:User

標籤:

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;//繼承objectc.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;}@Overridepublic 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 ....");}@Overridepublic 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;}@Overridepublic 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());//}}

 

java新手筆記18 類比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.