Classes, objects, and methods of the Java Foundation 9-java

Source: Internet
Author: User
Tags dota

First, what are classes, objects, and methods?

1. Class

The class is officially interpreted as: A class is a template that describes the behavior and state of a class of objects. The class itself is an abstract concept (we'll also introduce abstract classes later).

For example: There are a lot of heroes in DotA. These heroes (whether agile or power type, whether it is tin FAI or night dire) can be collectively known as the DotA heroes,DotA hero is a big category . Of course, we can continue to divide, for example, power type is a kind, agile is a kind. Tin Fai is a class, night dire is a class.

2. Properties or states of a class

From the above example, we find that heroes have names, blood levels, mana, number of skills, movement speed, and so on . These are the attributes or states of our class. Of course, the power or agile heroes may have different attributes, which involves inheritance, such as the day-FAI power heroes have the attributes of the dire hero, regardless of the days of the day or night have the characteristics of the DotA hero, specifically in the later detail.

// created a common class called Dotahero // declaration format of the class /* public class class name {}class class name {} */  Public class dotahero{    //Dotahero These attributes include name, blood, skill count, movement speed, etc.
Property Definition Format: Data Type property name
The data type can be either a base data type or a reference data type, and a string is a reference data type
String name; float HP; int skill; int movespeed;}

3. Objects

An object is an instance of a class, such as

We divide the DotA hero class, then we can create a real hero out, such as we create a hero called the Sword Saint, The sword Saint this hero is the DotA hero is an instantiated object of this class.

Create a good sword after the hero we can give him the name, the amount of blood and other properties, called initialization, if we do not set, then his value defaults to 0 or empty.

 Public classDotahero {String name;intskill;floatHP;intMovespeed;
Public Static voidMain (string[] args) {//object creation with the New keyword//format: Class name + object name = new class name ();Dotahero hero=NewDotahero ();//we have a name for this hero named Sword Saint .Hero.name= "Sword Saint";//Sword Saint's Blood volume is 600, the skill is 4, the movement speed is ten;hero.hp=600; Hero.skill=4; Hero.movespeed=110;//If we don't set these specific values, the default value for these properties is 0 or nullSystem.out.println ("Hero Name:" +hero.name+ ", Blood Volume:" +hero.hp+ ", Number of skills:" +Hero.skill); }}

So how did the JVM come to this execution?

4. Methods

A method is an action that an object can perform, which is a collection of statements written to perform a function.

For example: We created this sword Saint can attack, can release skills, can upgrade, can buy equipment, these are called methods.

 Public classDotahero {String name;intskill;floatHP;intMovespeed;  Public Static voidMain (string[] args) {Dotahero Hero=NewDotahero (); Hero.name= "Sword Saint"; HERO.HP=600; }//that's the way it declares an attack ./*method declaration format: public static return value type method name (parameter list) {program statement; [return[return value];]} The return value type must be the same if no return value is declared with void*/ Public Static voidattack () {//This is a no return value, no parameter methodSystem.out.println ("Normal attack"); }}

5. Invocation of the method

Method can be called in the Main method

 Public classDotahero {String name;intskill;floatHP;intMovespeed;  Public Static voidMain (string[] args) {Dotahero Hero=NewDotahero (); Hero.name= "Sword Saint"; HERO.HP=600;//calling the attack methodattack (); } Public Static voidattack () {System.out.println (Attack);}}

public static return value type method name ( parameter list ) {

program Statements ;

[return[ return value ];]

}

Classes, objects, and methods of the Java Foundation 9-java

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.