Java Novice Note Class 18 comparison

Source: Internet
Author: User

1, Shap class

Package Com.yfs.javase;public class Shape/*extends object */{  //default Inherit Object  object method all inherit//Calculate Area method public double Getarea () {System.out.println ("calculated area"); return 0;}}

2.Circle class

Package Com.yfs.javase;public Class Circle extends Shape {private double r;public Circle (double r) {THIS.R = R; SYSTEM.OUT.PRINTLN ("Create circular area"); Public double Getarea () {//override method System.out.println for parent class ("Calculate circular area ..."); return 3.14 * R * r;}}

3.Rangton class

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 ("Create rectangular area"); Public double Getarea () {System.out.println ("Calculate Rectangle area ..."); return width * length;}}

4.Trantangel class

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 ("Create Triangle area"); Public double Getarea () {System.out.println ("Calculate Triangle area ..."), return 1.0/2 * width * height;}

5.Shap Test

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 ();//base class//Method for all objects 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;//inherits Objectc.getclass (); Method (shape);//method (new Date ());} public static void Method (Object obj) {//obj.getarea (); Shape s = (shape) obj;s.getarea ();}}

6.Person Class (custom comparison)

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. int) {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.) {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.equ ALS (other.name)) return false;if (sex! = Other.sex) return False;return true; @Override//public int hashcode () {//return 123;//}////@Override//Custom class overrides the Equals method to define the object comparison rule//public Boolean equals (  Object obj) {//person other = (person) Obj;//return this.name.equals (other.name) && sex = = Other.sex && Age = = other.age;//}}

7.Person Test

Package Com.yfs.javase;public class Demo2 {/** * @param args */public static void main (string[] args) {person P1 = new Per Son (); System.out.println ("P1.hashcode ():" + P1.hashcode ()); person P2 = new Person (),//hashcode object's hash code System.out.println ("P2.hashcode ():" + P2.hashcode ()); Person P3 = new person (); System.out.println ("P3.hashcode ():" + P3.hashcode ());p 1.sayHello ();p 2.sayHello ();p 3.sayHello (); System.out.println ("P1 = = p2?" + (P1 = = p2));}}

8.Person class

Package Com.yfs.javase;public class Demo3 {/** * @param args */public static void main (string[] args) {person P1 = new Per Son ("Zhang San", ' Male ', 20); person P2 = new Person ("Zhang San", ' Male ', 20); System.out.println ("P1 = = P2:" + (P1 = = p2));//Compare whether the properties in the object are equal to 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");//sys Tem.out.println ("S1 = = S2:" + (S1 = = s2))//system.out.println ("S1.equals (S2):" + s1.equals (S2));//string class overrides Equals method O Bject obj = p1; System.out.println (Obj.getclass (). GetName ());/}}

Java Novice Note Class 18 comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.