Objects and classes

Source: Internet
Author: User

1. Concepts of objects and classes:

Object--The description of the objects in the problem domain by the computer language, and the objects are represented by properties and methods to represent the static and dynamic properties of things.

Class-Class is an abstract concept used to describe an object of the same type, which defines the static and dynamic properties of this class of objects.

A class can be seen as an abstraction of an object, which is a concrete instance of a class.

2. Creation and use of classes and objects: (code instance)

Class: Animail.java

Create public class Animail {        //global variable    private int age;    private String name;        Add method public    int Getage () {        return age;    }    public void Setage (int.) {        this.age = age;    }    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }        Add method public    void Print () {        static int money = 3000;//member variable, scoped to the Method field, must first be assigned, and then use        System.out.println (" The general method performed ");    }        /     * Construction method does not return     a value * When no construction method is added, the system defaults to adding a non-parametric constructor to     the class * When you add a method with a parameter construction, the system will no longer add an parameterless construction method to the class *     //    /No parameter construction method Public    Animail () {        System.out.println ("No parameter construction method executed");    }    There is a constructor method public    animail (int age) {        System.out.println ("The constructor method executed the age=" +age);}    }

Object: Test.java

public class Test {    /**     * @param class and Object */public    static void Main (string[] args) {        Animail animail = NE W Animail ();//creation of the object, execution of the parameterless constructor method of the class        animail animail2 = new Animail (1);//creation of the object, execution of the method//method of the constructor of the class, executed        by the object name point method name        Animail.setage (ten);        Animail.setname ("Kitten");        System.out.println ("Name=" +animail.getname () + "   age=" +animail.getage ());        Animail. Print ();}    }

3. This, static, package, import, access control

This: Currently, when modifying a global variable in a class, when the parameter name is consistent with the global variable name, the This keyword is used to modify the property of the current object.

Static: A global, static member variable declared with static, which is the common variable of the class, initialized on first use, and only one copy of the static member variable for all the multiple images of the class.

Package: Java in order to prevent user name duplication, resulting in an error, the concept of adding a package, the name of the package is generally the company domain name flashback.

Import: Reference, when you need to use a method within another package, you need to use the Import keyword to bring the package in.

Access control for Java is divided into private (private), default, protected (unlisted), Public (open)

  

Objects and classes

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.