Java Basics/Eighth lesson: Object-Oriented/Java class methods and instance methods

Source: Internet
Author: User
Tags instance method

2018-03-06

First, the method of the class:

In Java, "functions" in C + + + are called "methods", which means "How to do something."

The composition of the method: name, parameter, return value, and method body.

public class Hero {      String name;  //姓名           float hp;  //血量            float armor;  //护甲            int moveSpeed;  //移动速度      //获取护甲值      float getArmor(/*Argument list*/){       //float是调用方法后返回的数据类型。Argument list: The type and name of the method name and parameter list that defines the information passed to the method together uniquely identifies a method.          return armor;      }      //坑队友      void keng(){Void is no return value          System.out.println( "坑队友!" );      } }

Methods in Java can only be created as part of a class, and are invoked through an object. Attempting to invoke a method that does not exist on an object will give you a compile error message.

By Object Invocation Method:

Objectname.methodname (Arg1,arg2,arg3,...);

Formal parameters: Used to receive externally-passed data variables when a method is invoked

Parameter type: Data type of formal parameter

Return value: Method is returned to the program data that called it after execution is completed

Return value type: Method The data type of the result to return

Arguments: Data that is actually passed to the method form parameter when the method is called

Second, the example method:

Method declarations are called class methods that are decorated with static, and are called instance methods without static adornments.

The difference between a class method and an instance method:

Methods in a class can be called to each other, where instance methods can invoke instance methods or class methods in the class;

However, a class method can only invoke a class method in the class, and cannot invoke an instance method;

All member variables, including instance variables or class variables, can be called in an instance method.

However, class methods can only manipulate class variables. The instance method must be called through the object, and the class method can be called directly from the class name.

Java Basics/Eighth lesson: Object-Oriented/Java class methods and instance methods

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.