Classes and objects in Java programming

Source: Internet
Author: User
Tags stub

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, Baidu Cloud Link: Http://pan.baidu.com/s/1slQWhCL Password: cfxo

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, which defines the class's keywords, people is the name of the class public string name;//The properties of the class public string sex;    public int age;

Methods of the classpublic 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 the object         p.eat ();//methods of invoking classes         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 objectSYSTEM.OUT.PRINTLN ("Class default constructor method, without parameters");}Public people (String name) {//constructor method with parametersSystem.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;//private attribute protected string sex; protected property int a=10;//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 scopePublic String name;//Common Propertiesprivate int age;//Property    protected String sex;//protected property     int a=10;     public static void Main (string[] args) {        //defined i is a local variable that can only be used within the for loop for this part         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 objectThis (23,43);}Public Dog (int a, int b) {TODO auto-generated Constructor stubThis.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 keywordint 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 nameThis.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);     }
7:java method Overloads (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;//Automatic 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

Classes and objects in Java programming

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.