Java class member methods (member functions), java Functions

Source: Internet
Author: User

Java class member methods (member functions), java Functions

 

Defines a Person class and adds the following member functions to improve the Person class.

1. speak member function: outputs "I am a good guy"

2. jisuan member function: You can enter n to calculate 1 + 2 +... + n

3. add member function: Calculate the sum of two numbers

Package demo1; public class Demo2 {public static void main (String [] args) {Person p1 = new Person (); // call the member function p1.speak (); p1.jisuan (100); p1.qiuhe (13, 11) ;}// defines the Person class Person {int age; String name; // member function 1 public void speak () {System. out. println ("I am a good guy");} // member function 2 public void jisuan (int n) {int result = 0; for (int I = 1; I <= n; I ++) {result + = I;} System. out. println ("result:" + result);} // member function 3 public void qiuhe (int num1, int num2) {int result = 0; result = num1 + num2; system. out. println ("result:" + result) ;}} View Code

 

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.