JAVA learning notes-Basic JAVA syntax (4)

Source: Internet
Author: User

I. Object Transformation

1. Upward Transformation: the reference of the parent class points to the object of the subclass (when the reference of the parent class points to the object of the subclass, you cannot use the members that come out of the subclass)

2. Downward Transformation: A simple sentence is forced type conversion.

Ii. toString ()

Default call status: 1. Use the System. out. println () console to display only strings. Therefore, convert the string type before printing.

2. When '+' is used to connect strings

  toStringMethod returns a string consisting of the Class Name (the object is an instance of the class), The at tag"@And the unsigned hexadecimal representation of the object's hash code.

We recommend that you rewrite the toString () method in the class you write. This will make our program more concise and the output interface more beautiful.

The toString () method rewritten by the following program makes the program output more beautiful: The Program specifies the toString output method, making the program output more beautiful

public class test{    public static void main(String[] args){        Person p1 = new Person("jack",34);        Person p2 = new Person("tom",21);                System.out.println(p1);        System.out.println(p2);    }}class Person{    Person(String name,int age){        this.name = name;        this.age = age;    }        String name;    int age;    public String toString(){        return ("name is:" + name + "  age is:"+ age);    }}

Iii. Differences between "=" and "equals" and their usage

"=" Compares the stack content, while equals compares the stack content.

Generally, it is best to override the equals method when writing a class. In this way, you can compare objects to meet your own needs to implement user-related functions.

  However, if we overwrite the equals method, we must also overwrite the hashCode () method.

Equal objects must have equal hash codes. Equal hash codes do not necessarily have two equal objects (because a region with the same hash code in memory can have many objects)

For example, the equals method of Program Rewriting: we can define the comparison method of equals.

public class test{    public static void main(String[] args){        Person p1 = new Person("jack",21);        Person p2 = new Person("tom",23);        System.out.println(p1.equals(p2));    }}class Person{    Person(String name,int age){        this.name = name;        this.age = age;    }        String name;    int age;    public boolean equals(Object obj){        Person p = (Person)obj;        if(this.name.equals(p.name)){            return true;        }else{            return false;        }    }
  public int hashCode(){
    return this.name.hashCode();
}}

4. final keywords

1. Add a member variable to indicate a constant.

2. Add the method to indicate that the method cannot be overwritten.

3. When added to a class, it indicates that the class cannot be inherited.

5. abstract)

1. The class cannot be instantiated (the object cannot be created)

2. A class is used for inheritance. For example, when the parent class is referenced to a subclass object during polymorphism, the parent class does not need to be instantiated.

3. abstract methods are used for rewriting, and methods are abstract, so classes must be abstract.

4. abstract classes can have abstract methods or non-abstract methods.

Vi. interface)

1. An interface is a special abstract class (only abstract methods and constants)

2. It is a standard

3. interfaces are called classes to implement interfaces, and methods are also called implementations.

4. interfaces and classes are called implementations. interfaces are called inheritance. interfaces can have multiple parent classes,

All the methods are default public abstract constants. All the constants are public fianl static by default.

The program details the presentation of several features of the interface:

Public class test {public static void main (String [] args) {Animel animel = new Animel (); animel. catching (); animel. sleep (); animel. run () ;}} interface cat {// write an interface public void catching ();} interface mouse {public void run (); // This is an abstract method} interface dog {int num = 10; // an abstract constant. Its detailed representation is: public fianl static int num = 10; public void see ();} interface pig extends cat, mouse, dog {// multi-inheritance. The inheritance between interfaces can have multiple public void sleep ();} class Animel implements pig {// only one parent class public void catching () {System is allowed for the implementation between the class Animel implements pig {// class and interface. out. println ("cat is catching");} public void run () {System. out. println ("mouse is running");} public void sleep () {System. out. println ("pig is sleeping");} public void see () {System. out. println ("dog is see ");}}

 

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.