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