J2se basics: 1. Class and object Basics

Source: Internet
Author: User
Tags type null variable scope
What is an object?
In Java, all people, things, or modules are an object.
The same object has the same features.

Dog, cat, and Snake (animal object)

Apple, pear, and orange (fruit object)


What is a class?
Abstract objects in real life
Such an abstract data type is called a class.
Animal (animal)
Fruit (fruit)


Relationship between classes and objects
A class is an object template (abstract representation), and an object is an instantiation of A Class (detailed presentation)


Class Structure

Java is purely object-oriented (except for eight basic data types)
Objects are generated from classes. Therefore, classes are the most basic and core components of Java programs.
Element.


Variable (attribute, member variable)

Method (function, member method, responsibility)

Message (communication between objects and transmission between object data)

Attribute Definition

Attribute Definition

Attribute Initial Value (default)
Booleanfalse
Byte/short/INT/long 0
Float 0.0f
Double 0.0d
Char '\ u0000'
Object Type null


Classification and scope of Variables
Class variable: The Class Object belongs to the object. Static modified variables are called class variables.
Static zone. It is instantiated only once, so it only occupies the memory once.

Object member variable: The member variable belongs to the object.
Heap area.

Local variable. The variables defined in the local method and the number of vertices in the form are all local variables.
Stack area.


Variable scope (lifecycle)

The time when different types of variables survive in the memory.


Classification of methods:
Divided by return values:
Return Value
No return value


Divided by shards:
Number of shards
No quota method


Void sum (int x, int y): Separate multiple values with commas.

Sum (x, y); // calls are also separated by commas.



Actual number of workers)

Form (number of vertices ).


When the number of arguments is passed, there are two ways to pass the real arguments to the form OPERATOR:

A: if the transfer is a basic data type, it is just a copy of the value.

B: If the object type is passed, the object address is passed.
By Function

Constructor

Instance method (Object method, member method)

Class Method

Constructor:

Is an operation that must be executed to instantiate an object.
This API is used to initialize a new object.

If there is no constructor, the system automatically creates an empty constructor.

If there is a constructor, the system will not be built.



Note:
Constructor is not a non-constructor.

Public Person (int id ){
}
Person = new person (); // Error
A: person = new pserson (1 );

B: Provide an empty constructor.


This:
Indicates the current object.
This indicates your reference.
This can be used to call the attributes and methods of the current object.




Method overload)

Method overloading means that multiple methods in the same class can have the same name.
However, the number of workers of these methods must be different (the number of workers is different or the number of workers is different ).


When calling, the compiler will use the number or type of workers to find which one to call.
Method.

Class methods, object methods, and constructor methods can be overloaded.


When the number of overloaded partitions is assumed to have an inheritance relationship. Unless it is precisely described in the program
Transfer Type. Otherwise, all methods that comply with the number of workers of the subclass are called.
Object OBJ = NULL; string STR = NULL; test. print (OBJ); // exact type test. print (STR); // exact type test. print (null); // The type that matches all. Call the subclass method. Public void print (Object some) {system. out. println ("Object version print");} public void print (string some) {system. out. println ("string version print ");}





/*** Define a student class */public class student {// part of the attribute declaration int stu_id; char stu_sex; string stu_name; // class variable static string class_id = "jn1001"; // method with return value int getscore () {// defines the local variable int English = 0; return English ;} int getscope2 () {double math = 12.345; Return (INT) math;} // void Study () {system. out. println ("Everyone is learning Java");} public static void main (string ARGs []) {student Stu = new student (); Stu. study (); int result = Stu. getscore (); int result2 = Stu. getscope2 (); system. out. println ("result =" + Result + ", result2 =" + result2 );}}

Public class student_method {// constructor public student_method () {system. out. println ("object constructor");} // object (member) method int divde (int x, int y) {return x/y ;} // class method static int sum (int x, int y) {return X + Y;} public static void main (string ARGs []) {// call the class method student_method.sum (2, 3); // call the member method // student_method.divde (2, 3); // The system cannot be called. out. println ("1111"); student_method test = new student_method (); system. out. println ("222"); test. divde (3, 4 );}}


public class TestOverLoad{public static void main(String[] args){Test test = new Test();//test.print(null); Object obj = null;String str = null;StringBuffer strBuffer = null;System.out.println(obj);System.out.println(str);test.print(obj);test.print(str);test.print(strBuffer);test.print(null);} }class Test{public void print(Object some){System.out.println("Object version print"); }public void print(String some){System.out.println("String version print");} public void print(StringBuffer some){System.out.println("StringBuffer version print"); }}



J2se basics: 1. Class and object Basics

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.