J2se-object-oriented

Source: Internet
Author: User
Tags float double
View code

 Public   Static   Void Main (string [] ARGs) Throws  Exception {  //  Createuserrolefield. getinstance (); List = New Arraylist <userrolevo> (); Userrolevo u = New  Userrolevo (); U. setidcard ( "1"); U. setusername ( "Name1" ); U. setrolename ( "Rolename1" ); List. Add (U); U. setidcard ( "2" ); U. setusername ( "Name2" ); U. setrolename ( "Rolename2" ); List. Add (U); U. setidcard ( "3" ); U. setusername ( "Name3" ); U. setrolename ( "Rolename3" ); List. Add (U );  For (Userrolevo urv: List ){  //  The output is always 3, because the U reference eventually points to the third initialization.  System. Out. println (urv. getidcard ());}} 

 

Data Type:
Basic capacity from small to large type: byte short char int long float double (Boolean cannot be converted to each other)
Small capacity is automatically converted to large
When the capacity is large, a forced conversion character must be added, which may reduce the precision or overflow.
When there are multiple types of data mixed operations,First, the system automatically converts all data to the data type with the largest capacity.And then compute

Data Transformation:
Whether the instance of is of a certain type
Animal A = new dog ();
DOG d = (DOG) A; (mandatory conversion character must be added when the capacity is large to small.) D. New dog attributes can be seen.
The subclass object can be used as the base class object, which is called upcasting, and vice versa.

Static keywords:
The static declared member variable is a static member variable, which is a public variable of the class and is initialized for the first time. For all objects of the class, the static member variable

Only one copy.
The static declared method is a static method. When calling this method, it is not called for an object, so it cannot access non-static members.
You can access static members through object references or class names.

Access control:
The modifier class contains the same package subclass anywhere.
Private
Default is acceptable.
Protected can be used.
Public: Yes.

Abstract class:
When an abstract keyword is used to modify a class, this class is called an abstract class (compared with the interface, some methods have been very refined and do not need to be rewritten, but some of them need to be rewritten, that is, an abstract class, all must be rewritten as interfaces)
Abstract method: When abstract is used to modify a method, this method is called abstract method.
Feature: Classes containing abstract methods must be declared as abstract classes, abstract classes must be inherited, abstract methods must be overwritten, or subclasses must also be declared as abstract methods.
Abstract classes cannot be instantiated.
Abstract METHODS only need to be declared and not implemented

Interface:
Is a set of definitions of abstract methods and constant values.
Essentially, an interface is a special abstract class that only contains the definition of constants and methods, without the implementation of variables and methods. (An interface is a specification that provides the functions of a class. It only describes the functions of the class. The specific work is the implementation method. Static methods do not need to be written in interfaces. If there is a specific business need, it is better to use abstract classes, it is against the Interface Usage)
The declared attribute in the interface is public static final by default, and can only be public static final,
Public; expands the scalability of the interface and can be accessed anywhere.
Static: implemented over and over again, which is static. One reduces memory consumption, and the other has attributes with the same name when multiple interfaces are implemented.
If it is defined as static, the corresponding "same" attribute can be found through the class name without conflict.
Final: If you want to implement anything, you need to follow the standards defined in my interface, otherwise it will not work! If the attribute is not set to final,
Then, all the classes that implement the interface can define a method and then modify this attribute, so the standard meaning of the original interface definition does not exist.

The method declared in the interface is public abstract by default.
Public
Abstract: an interface is a rule that is implemented by others.

Interfaces can be inherited from each other, but they cannot be implemented (all interfaces. Method bodies cannot appear in methods. How can we implement them)
 

Polymorphism:
Inheritance required
Rewrite required
Parent class references to child classes

Final:
Read-only attribute
Method cannot be overwritten
Class cannot be inherited

 

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.