吴裕雄--天生自然 JAVA开发学习:方法

/** 返回两个整型变量数据的较大值 */
public static int max(int num1, int num2) {
   int result;
   if (num1 > num2)
      result = num1;
   else
      result = num2;

   return result;
}
public class TestMax {
   /** 主方法 */
   public static void main(String[] args) {
      int i = 5;
      int j = 2;
      int k = max(i, j);
      System.out.println( i + " 和 " + j + " 比较,最大值是:" + k);
   }

   /** 返回两个整数变量较大的值 */
   public static int max(int num1, int num2) {
      int result;
      if (num1 > num2)
         result = num1;
      else
         result = num2;

      return result;
   }
}
public class TestVoidMethod {
  public static void main(String[] args) {
    printGrade(78.5);
  }

  public static void printGrade(double score) {
    if (score >= 90.0) {
       System.out.println(‘A‘);
    }
    else if (score >= 80.0) {
       System.out.println(‘B‘);
    }
    else if (score >= 70.0) {
       System.out.println(‘C‘);
    }
    else if (score >= 60.0) {
       System.out.println(‘D‘);
    }
    else {
       System.out.println(‘F‘);
    }
  }
}
public static void nPrintln(String message, int n) {
  for (int i = 0; i < n; i++) {
    System.out.println(message);
  }
}
public class TestPassByValue {
  public static void main(String[] args) {
    int num1 = 1;
    int num2 = 2;

    System.out.println("交换前 num1 的值为:" +
                        num1 + " ,num2 的值为:" + num2);

    // 调用swap方法
    swap(num1, num2);
    System.out.println("交换后 num1 的值为:" +
                       num1 + " ,num2 的值为:" + num2);
  }
  /** 交换两个变量的方法 */
  public static void swap(int n1, int n2) {
    System.out.println("\t进入 swap 方法");
    System.out.println("\t\t交换前 n1 的值为:" + n1
                         + ",n2 的值:" + n2);
    // 交换 n1 与 n2的值
    int temp = n1;
    n1 = n2;
    n2 = temp;

    System.out.println("\t\t交换后 n1 的值为 " + n1
                         + ",n2 的值:" + n2);
  }
}

// 一个简单的构造函数
class MyClass {
  int x;

  // 以下是构造函数
  MyClass() {
    x = 10;
  }
}
// 一个简单的构造函数
class MyClass {
  int x;

  // 以下是构造函数
  MyClass(int i ) {
    x = i;
  }
}
public class ConsDemo {
  public static void main(String args[]) {
    MyClass t1 = new MyClass( 10 );
    MyClass t2 = new MyClass( 20 );
    System.out.println(t1.x + " " + t2.x);
  }
}
public class VarargsDemo {
    public static void main(String args[]) {
        // 调用可变参数的方法
        printMax(34, 3, 3, 2, 56.5);
        printMax(new double[]{1, 2, 3});
    }

    public static void printMax( double... numbers) {
        if (numbers.length == 0) {
            System.out.println("No argument passed");
            return;
        }

        double result = numbers[0];

        for (int i = 1; i <  numbers.length; i++){
            if (numbers[i] >  result) {
                result = numbers[i];
            }
        }
        System.out.println("The max value is " + result);
    }
}
public class FinalizationDemo {
  public static void main(String[] args) {
    Cake c1 = new Cake(1);
    Cake c2 = new Cake(2);
    Cake c3 = new Cake(3);  

    c2 = c3 = null;
    System.gc(); //调用Java垃圾收集器
  }
}  

class Cake extends Object {
  private int id;
  public Cake(int id) {
    this.id = id;
    System.out.println("Cake Object " + id + "is created");
  }  

  protected void finalize() throws java.lang.Throwable {
    super.finalize();
    System.out.println("Cake Object " + id + "is disposed");
  }
}

原文地址:https://www.cnblogs.com/tszr/p/10962888.html

时间: 2024-07-31 16:53:30

吴裕雄--天生自然 JAVA开发学习:方法的相关文章

吴裕雄--天生自然 JAVA开发学习:变量类型

public class Variable{ static int allClicks=0; // 类变量 String str="hello world"; // 实例变量 public void method(){ int i =0; // 局部变量 } } public class Test{ public void pupAge(){ int age = 0; age = age + 7; System.out.println("小狗的年龄是: " + ag

吴裕雄--天生自然 JAVA开发学习:异常处理

try { // 程序代码 }catch(ExceptionName e1) { //Catch 块 } import java.io.*; public class ExcepTest{ public static void main(String args[]){ try{ int a[] = new int[2]; System.out.println("Access element three :" + a[3]); }catch(ArrayIndexOutOfBoundsEx

吴裕雄--天生自然 JAVA开发学习:集合框架

import java.util.*; public class Test{ public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("Hello"); list.add("World"); list.add("HAHAHAHA"); //第一种遍历方法使用foreach遍历List for (

吴裕雄--天生自然 JAVA开发学习:基本数据类型

public class PrimitiveTypeTest { public static void main(String[] args) { // byte System.out.println("基本类型:byte 二进制位数:" + Byte.SIZE); System.out.println("包装类:java.lang.Byte"); System.out.println("最小值:Byte.MIN_VALUE=" + Byte.M

吴裕雄--天生自然 JAVA开发学习:日期时间

import java.util.Date; public class DateDemo { public static void main(String args[]) { // 初始化 Date 对象 Date date = new Date(); // 使用 toString() 函数显示日期时间 System.out.println(date.toString()); } } import java.util.*; import java.text.*; public class Dat

吴裕雄--天生自然 JAVA开发学习:正则表达式

import java.util.regex.*; class RegexExample1{ public static void main(String args[]){ String content = "I am noob " + "from runoob.com."; String pattern = ".*runoob.*"; boolean isMatch = Pattern.matches(pattern, content); Sy

吴裕雄--天生自然 JAVA开发学习:流(Stream)、文件(File)和IO

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //使用 BufferedReader 在控制台读取字符 import java.io.*; public class BRRead { public static void main(String args[]) throws IOException { char c; // 使用 System.in 创建 BufferedReader Buffe

吴裕雄--天生自然 JAVA开发学习:继承

class 父类 { } class 子类 extends 父类 { } public class Penguin { private String name; private int id; public Penguin(String myName, int myid) { name = myName; id = myid; } public void eat(){ System.out.println(name+"正在吃"); } public void sleep(){ Syst

吴裕雄--天生自然 JAVA开发学习:多态

Parent p = new Child(); public class Test { public static void main(String[] args) { show(new Cat()); // 以 Cat 对象调用 show 方法 show(new Dog()); // 以 Dog 对象调用 show 方法 Animal a = new Cat(); // 向上转型 a.eat(); // 调用的是 Cat 的 eat Cat c = (Cat)a; // 向下转型 c.work