类是对象的属性和行为的载体。

对象的属性——类的成员变量

对象的行为——类的成员方法 

class 类的名称{
//类的成员变量  可不设初始值,默认为:0,0.0,'',null,false。
//类的成员方法
[权限修饰符][返回值类型]方法名 ([参数类型 参数名])[throws 异常类型]{
//方法体
……
return 返回值;
}
}

方法

权限修饰符

private

public

protected

为空:只在本类及同一个包中的类进行访问

返回值类型

如果无返回,则使用关键字void。

成员方法的参数

形参:在方法内部接收实参的变量;实参:传给方法的值

 值参数:表示形参和实参之间按值传递。形参的改变,不影响实参的值。

引用参数:被调用后,原值被修改的参数 ,如下面的usd即为引用参数

不定长参数:声明方法时使用,格式:参数类型… 参数名称

 

引用参数

public class ExchangeRate { // 创建汇率类

    public static void main(String[] args) {
        ExchangeRate exr = new ExchangeRate();
        double[] usd = {1, 100, 1000};
        System.out.println("美元记录:");
        exr.printList(usd, "美元");//调用打印方法
        exr.exChange(usd);//调用兑换方法
        System.out.println("兑换完毕:");
        exr.printList(usd, "元");//调用打印方法

    }

    public void exChange(double[] usd) {  //引用参数后值被更改
//        兑换
        for (int i = 0; i < usd.length; i++) {
            usd[i] = usd[i] * 6.903;
        }
        return usd;
    }

    public void printList(double[] usd, String unit) {
//        打印list
        for (int i = 0; i < usd.length; i++) {
            if (i == usd.length - 1) {
                System.out.println(usd[i] + unit);//最后一个换行打印
            } else {
                System.out.print(usd[i] + unit + " "); //非最后一个不换行打印
            }

        }
    }
}

 

import javax.jws.soap.SOAPBinding;
import java.util.Scanner;

public class BuyBooks {

    public static void main(String[] args) {
        BuyBooks bb = new BuyBooks();
        System.out.println("1  《A》 59.8\n2  《B》 69.8\n3  《C》 20.0\n4  《D》 40.0\n请输入预购买的序号:使用,隔开");
        Scanner sc = new Scanner(System.in);
        do {
            String nums = sc.next();
            if (nums.equals("0") ) {
                System.out.println("over");
                break;
            } else {
                String[] numlist = nums.split(",");//字符串转为数组

                int[] num = bb.getNum(numlist);//将输入的字符串转为int数字

                double sum = bb.disCount(num);//计算付费金额
                System.out.println("您要购买的有:【" + nums + "】,共计:" + sum + "元");
            }
        } while (true);


    }

    public int[] getNum(String[] numlist) {
        //将数组中的字符串转int数字
        int[] num = new int[numlist.length];
        for (int i = 0; i < numlist.length; i++) {
            num[i] = Integer.parseInt(numlist[i]); 
        }
        return num;
    }

    public double getPrice(int num) {
        // 通过序号获取价格
        double price = 0;
        switch (num) {
            case 1: {
                price = 59.8;
                break;
            }
            case 2: {
                price = 69.8;
                break;
            }
            case 3: {
                price = 20.0;
                break;
            }
            case 4: {
                price = 40.0;
                break;
            }
            default: {
                price = 0;
            }
        }
        return price;
    }


    public double disCount(int[] num) {
        //满2件5折,第单数件原价
        double sum = 0.0;
        for (int i = 0; i < num.length; i++) {
            if (num.length % 2 == 0) { //总件数为双数,均打折
                sum += BuyBooks.this.getPrice(num[i]) / 2;  //方法调用其他方法

            } else {
                if (i == num.length - 1) { //总件数为单数,最后一个不打折
                    sum += BuyBooks.this.getPrice(num[i]);
                } else {
                    sum += BuyBooks.this.getPrice(num[i]) / 2;
                }

            }

        }
        return sum;
    }
}

 

不定长参数

public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        int sum = test.add(10,20,33,44,55);
        System.out.println("sum");
    }
    public int add(int... x) {
        int sum = 0;
        for (int i = 0; i < x.length; i++) {
            sum += x[i];
        }
        return sum;
    }

 

posted on 2022-07-06 17:47  jxba  阅读(330)  评论(0编辑  收藏  举报