1 package test; 2 3 class A 4 { 5 private static int i; // Static, Private Attribute 6 private static int j; // Static, Private Attribute 7 private static int cnt = 0; // Statistic the number of the object 8 void set(int a , int b) // Set the value by the function in class 9 { // The security can be guaranteed 10 i = a; 11 j = b; 12 } 13 14 public A(int a , int b) // Construction Method 15 { 16 System.out.printf("The function has been called!\n"); 17 i = a; 18 j = b; 19 cnt++; 20 } 21 22 public static int Cnt() //Get the number of the object in this class 23 { //It should can be static because we can 24 return cnt; //A.Cnt 25 } 26 27 public void show() // A Show Method in the class 28 { 29 System.out.printf("The value in this object: i=%d,j=%d\n",i,j); 30 } 31 32 33 } 34 35 36 ///////////Extends///////////////////// 37 class Human 38 { 39 public String name = "Mike"; 40 public int age = 22; 41 } 42 43 class Student extends Human 44 { 45 public double score = 90; 46 } 47 48 class Graduate extends Student 49 { 50 public String tutor = "Jay"; 51 } 52 /*************Extends**********************/ 53 54 55 public class TestMemo { 56 57 static int add(int a ,int b) // The reentry of a function 58 { 59 return (a+b); 60 } 61 62 static int add(int a ,int b , int c) // The reentry of a function 63 { 64 return (a+b+c); 65 } 66 67 static double add(double a ,double b) // The reentry of a function 68 { 69 return a+b; 70 } 71 public static void main(String[] args) // The reentry of a function 72 { 73 A aa = new A(66,88); 74 // aa.i = 100; 75 // aa.j = 20; 76 //aa.set(50, 67); 77 aa.show(); 78 System.out.printf("Two int value be plused:%d\n",add(2,8)); 79 System.out.printf("Three int value be plused:%d\n",add(1,2,3)); 80 System.out.printf("Two float value be plused:%f\n",add(1.9,2.0)); 81 A bb = new A(12,10); //change the value in the class by another object 82 aa.show(); // because of the static attribute 83 System.out.printf("The vaule count in A class: %d\n",A.Cnt()); 84 Graduate stu = new Graduate(); 85 System.out.printf("Test of EXTENDS: %s‘s tutor is %s\n",stu.name,stu.tutor); 86 System.out.printf("Test of EXTENDS: %s‘s age is %d\n",stu.name,stu.age); 87 } 88 }
时间: 2024-11-04 13:08:58