ACCP8.0 First Semester Java course-about classes and objects

Source: Internet
Author: User

This chapter highlights
1. Mastering Classes and objects
2. Understanding Encapsulation
3. Classes and objects are created

The difficulty of this chapter
Creating Classes and objects

An understanding Class
1. Class describes the characteristics and behavior of things
2. A class can also be understood as a collection of things of the same character and the same behavior
3. The class is a template, which can be described by a number of specific examples (such as: personal resume template, we will be able to fill out a lot of full resume after completion)
4. A class is a generic term, such as when it comes to a noun or thing, and the concept in the mind is vague.
For example: Humans, animals, these words are a series of vague nouns, they are all classes
5. Each class has some characteristics that we call attributes
6. Each class has some behavior that we call a method

Two objects of understanding
2.1. An object is a concrete instance of a class
2.2. Class is a generic, object is specified
2.3. Each object is created by a class

Three definition classes
3.1. Define class name (class name cannot use keywords, cannot contain spaces and special characters except underscores and "$" symbols, cannot start with a number)
3.2. The first letter must be capitalized according to the specification
3.3. Writing the properties of a class
3.4. Methods for writing classes

Four defining the properties of a class
4.1 attribute refers to the characteristics of the class, for example, we write a resume with a name, address, etc. to fill in personal information is equivalent to the property
The 4.2 property, also known as a member variable, has the syntax structure: access modifier data type variable name
4.3 attribute members They will have default values if they are not assigned int-> 0,double->0.0 boolean->false,char-> '/u0000 ', string->null
4.4 Properties can be accessed throughout the class. This is different from local variables

Five ways to define classes
The 5.1 method focuses on the behavior of a class
5.2 Definition method syntax is the access modifier return value method name (parameter type argument name) {}

Six Understanding Encapsulation
6.1 Package Specifically, the implementation of internal masking details, external methods used to invoke, when to complete a function, we do not need to understand how this is implemented, just need to know to call this method to implement this function is good

To illustrate:
1  PackageCh11;2 3 /**4 * Student class, grammatical structure:5 * Public class class name {6 *//Properties7  *     8  *     9 *//MethodTen  * } One * Created by Niechen on 17/5/13. A  */ - classStudent { -     /* the * syntax for defining attributes: - * Access modifier data type variable name -      *  - * If no access modifier is the default access modifier (friendly) +      * -      */ +     intAge//Age AString name;//name at  -  -     /** - * Define the syntax structure of the method: -      *  - * Access modifier return value method name () { in *//Method Body -      * } to      *  +      */ -      Public voidPlay () { the         if(Age < 18) { *System.out.printf ("%s is too young to play games ...", name); $}Else{Panax NotoginsengSystem.out.printf ("%s is playing a game ...", name); -         } the     } +}

Objects must be created after use:

1  PackageCh11;2 3 /** 4 * Created by Niechen on 17/5/13.5  */6  Public classMain {7      Public Static voidMain (string[] args) {8Student Student =NewStudent (); //class is the type of the object (student the data type of the variable is student type) 9Student.name = "Zhang San"; //attribute assignment TenStudent.age = 20; One Student.play (); ///Call method, caller does not need to know how to implement the play method, just need to know that the call play method can play this function.  A     } -}

ACCP8.0 First Semester Java course-about 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.