05-java Classes and objects

Source: Internet
Author: User

1: Beginners Java, all know that Java is Object-oriented programming. The author begins this section by talking about classes and objects. (Example for reference only, if copy and paste remember to modify the package name and class name, to avoid errors)

Learn Java shortcut keys, alt+/code completion function, in fact, this shortcut started the Eclipse Code Auxiliary menu, Shift+ctrl+o shortcut key Import Package

Ctrl+f can quickly find the API, the Chinese version of the API.

First of all

Object creation: Class Name Object name =new class name (); Perhaps most of the learning of Java will be written, but the understanding is like this,

New class name, you get an object, and the type of the object is the type of the class name.

For example: Car car=new car (),//is to get the car object by new car class name, and this object is car type

2: The class must be defined before it can be used. The class is the template that creates the object, and the object that is created is also called the instantiation of the class.

Package com.cnblogs;//defines the format of packages, keyword package, the name of the most canonical format is the domain name of the anti-write, such as com. what

public class People {//class keyword, defining the class's keyword, people is the name of the class

Public String name;//properties of the class
public String sex;
public int age;

Methods of the class
public void sleep () {
System.out.println ("People like to sleep when they are tired");
}

public void Eat () {
System.out.println ("People like to eat dinner when they are hungry");
}
public static void Main (string[] args) {//main function
People p=new people ();//Instantiation of Object
P.eat ();//method that invokes the class
P.sleep ();
}

}
3: Construction method

The name of the construction method must be the same as the name of the class, and there is no return value.
Each class has a constructor method. If you do not explicitly define a construction method for a class, the Java compiler will provide a default constructor for that class.

Package com.cnblogs;

public class People {

public String name;
public String sex;
public int age;

Public people () {//default constructor method, call default constructor when initializing object
SYSTEM.OUT.PRINTLN ("Class default constructor method, without parameters");
}
Public people (String name) {//constructor method with parameters
System.out.println ("Method of construction with parameters");
}
public void sleep () {
System.out.println ("People like to sleep when they are tired");
}

public void Eat () {
System.out.println ("People like to eat dinner when they are hungry");
}
public static void Main (string[] args) {
People p=new people ();
P.eat ();
P.sleep ();
}

}

4:java uses modifiers to control access to classes, properties, and methods, and other functions, usually at the forefront of the statement.

The permission (access) modifier keyword contains: public, private, protected, and the other one is the default;

Public: Visible to all classes, including this class, same package other class or subclass, other package class or subclass

Protected: Visible to this class, other classes or subclasses of the same package are visible, the class or subclass of the other package cannot be seen

Private: Only visible to this class, to other classes or subclasses of the same package is not visible to the other package class or subclass can not see

Default: Is visible within the same package

Examples are as follows:

Package com.cnblogs;

public class Men {

Public String name;//Common Properties
private int age;//Property
Protected String sex;//protected property
int a=10;//the default
}

5: Scope of the variable, including all variables and local variables

Examples are as follows

Package com.cnblogs;

public class Men {
All variables, which can be used throughout the scope
Public String name;//Common Properties
private int age;//Property
Protected String sex;//protected property
int a=10;
public static void Main (string[] args) {
The defined I is a local variable that can only be used within the For loop
for (int i=0;i<10;i++) {
SYSTEM.OUT.PRINTLN ("Local variables, which can be used locally" +i);
}
}

}

6:this keywords

The This keyword is used to represent the current object itself, or an instance of the current class, through which all methods and properties of this object can be called.

How do you want to call member variables inside a method when the member variable has the same name as the variable inside the method? Only this can be used at this time.

Initializes an object as a method name, which is equivalent to invoking the other constructor of this class, which must be the first sentence of the constructor method.

As a parameter pass, you need to call a method in some completely detached class and pass a reference to the current object as a parameter.

Examples are as follows

Package com.cnblogs;

public class Dog {

int a=21;
int b=12;

Public Dog () {//this keyword as the method name to initialize the object
This (23,43);
}

Public Dog (int a, int b) {
TODO auto-generated Constructor stub
This.a=a;
This.b=b;
System.out.println (A + "" +b);
}

public void Add () {//the properties and methods of the class can be called through the This keyword
int c=this.a+this.b;
System.out.println (c);
}

public void SetA (int a) {//Use the This keyword to differentiate a variable with the same name
This.a=a;
}
public int Geta () {
return A;
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
Dog D=new Dog ();
D.add ();

D.seta (44);
int Dd=d.geta ();
SYSTEM.OUT.PRINTLN (DD);
}

}
Overloads of methods in 7:java (construction methods can also be overloaded)

That is, the method's name is the same, and the method's parameter type, number, and order are the overloads of the method.

Note 1: A method declared as final cannot be overloaded, and a method declared as static cannot be overloaded, but can be declared again.

Note 2: Only the return value types are different and cannot constitute overloads of the method

Examples are as follows:

Package com.cnblogs;

public class Cat {

public void Cat () {
System.out.println ("Overloading of methods");
}
public void cat (int a,int b) {
System.out.println ("Overloads of methods, number of parameters in a method");
}
public void Cat (String c,int D) {
System.out.println ("Overloads of methods, different types of parameters in methods");
}
public void Cat (int e,string f) {
System.out.println ("overloaded method, different parameter order in method");
}

public static void Main (string[] args) {
TODO auto-generated Method Stub

}

}

8: Packing class (Wrapper Classes), unpacking and packing


The basic types and corresponding wrapper classes can be swapped with each other:

The conversion from the basic type to the corresponding wrapper class is called boxing, for example, a double is wrapped into a double class object;

The wrapper class translates to the corresponding base type as a unboxing, such as simplifying the object of the Integer class to int.

Package com.cnblogs;

public class Sun {

public static void Main (string[] args) {
TODO auto-generated Method Stub
int a=32;
Integer i=new integer (a);
System.out.println ("manual Boxing" +i);

int A2=i.intvalue ();//The function of this method is to return this integer object with an int value
System.out.println ("Manual Unpacking" +A2);

int a4=54;
Integer i4=a4;//Auto-boxing
SYSTEM.OUT.PRINTLN ("Automatic Boxing" +i4);
}

}

Key notes to enhance understanding of polymorphic meanings

Three prerequisites for polymorphic presence:

    • Inherited
    • Rewrite
    • Parent class reference to child class object

05-java Classes and objects

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.