"Effective Java" Learning notes (4)

Source: Internet
Author: User
Tags final interface variables return string
Notes
Five. The substitution of C language structure

How to simulate some of the structures in C language with Java

19th: replacing structures with classes

For structures in C, Java can be replaced with classes, however, you should not let class member variables can be publicly accessed, member variables should be private, and then provide some read and write operations to manipulate these variables, compared to C or C + + structure type, such class is more secure, embodies the encapsulation of OOP. Sample code

public class point{

private float X;

private float y;

public float GetX () {return x;}

public float GetY () {return y;}

public void SetX (int x) {this.x=x}

public void sety (int y) {this.y=y}

}

20th: Use class level to replace Union

For this, I do not elaborate, interested in their own to see how the United is going. The book is a famous shape example

21st: Replace the enumeration structure with the class

Java cancels the enumeration type in C language (note: jdk5.0 has added this feature and is interested in looking up the data for himself), and here is how to simulate this structure before 5.0.

C Language: typedef enum{Circle,rectangle,square} shape;

Java language:

public class shape{

Private final String name;

Public shape (String name) {this.name=name;}

Public String toString () {return name;}

public static final Shape circle=new shape ("CIRCLE");

public static final Shape rectangle=new shape ("RECTANGLE");

public static final Shape square=new shape ("SQUARE");

}

You can use shape later. Circle,shape. Square to access the variables, you can see that the Java implementation model is more secure and it has a type check. It's a beautiful design!

22nd: Using classes and interfaces to replace function pointers

Java does not have functions that are independent of classes or interfaces, and any method is contained within a class or instance, so the function pointers in C can be completely replaced with classes and interfaces, and obviously examples such as the Compareable interface, an array that needs to be sorted, are called

Arrays.sort (), you can pass in a comparer that implements the Compareable interface. This is the equivalent of a function pointer.




Related Article

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.