• 关于JAVA核心技术(卷一)读后的思考(用户自定义类,静态域和静态方法的思考以及方法参数)


    用户自定义类:

    这部分并没有太过于困难的部分,借由代码进行复习:

    Employee类的定义:

    package com.java.EmployeeTest;

    import java.time.*;

    public class Employee {
        private String name;
        private double salary;
        private LocalDate hireDay;//以上分别是Employee类的实例域
        
        public Employee(String n,double s,int year,int month,int day) {
            name=n;
            salary=s;
            this.hireDay=LocalDate.of(year, month, day);
        }
        public String getName() {
            return name;
            
        }
        public double getSalary() {
            return salary;
        }
        public LocalDate getHireDay() {
            
            return hireDay;
            
        }
        
        public void raiseSalary(double byPercent) {
            double raise =salary*byPercent/100;
            salary += raise;
        }//方法,因为实例域为保证安全性都是用private类型,要引用需要用public方法进行调用

    }

    main函数:

    package com.java.EmployeeTest;
    import java.time.*;


    public class EmployeeTest {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Employee[] staff = new Employee[3];
            staff[0] = new Employee("Carl Cracker",75000,1987,12,15);
            staff[1] = new Employee("Harry Hacker",50000,1989,10,1);
            staff[2] = new Employee("Tony Tester",40000,1990,3,15);
            
            for(Employee e:staff)
                e.raiseSalary(5);
            
            for(Employee e: staff)
                System.out.println("name="+e.getName()+",salary="+e.getSalary()+",hireDay="+e.getHireDay());

        }

    }
    没啥说的,这部分内容完全可以自己学习。

    静态语和静态方法(static)

    静态域

    现给每个对象添加唯一标识码,nextId(静态域),id

    class Employee{

    private static int nextId=1;

    private Int id;

    }

    每个对象都拥有自己的id,但这个类所有的实例将共享一个nextId,它属于类,若无对象也存在。

    静态常量

    Math类中定义了一个静态常量:

    public class Math{

    ……

    public  static final double PI = 3.14159265358979323846;

    ……

    }

    在程序中,可以直接调用类来获得常量,即Math.PI

    另一个多次使用的静态常态是System.out.。

    public class System{

    ……

    public static final PrintStream out =..;

    ......

    }

    静态方法

    静态方法是一种不能向对象实施操作的方法

    例如:Math类的pow方法就是一个静态方法。表达式:Math.pow(x,a);计算x的a次幂;

    可以认为静态方法是没有this参数的方法。

    附:若对静态域初始化代码比较复杂,可以用:

    static{

    ……

    ……

    ……}

    这样表示

    重点:静态方法中是不能调用非静态域。形参可以和非静态变量一样,但并非表达同一意思。

    代码一览:

    package com.java.StaticTest;

    public class Employee {
        private static int nextId=1;
        private String name;
        private double salary;
        private int id;
        
        public Employee (String n, double s) {
            name=n;
            salary=s;
            id=0;
            
        }
        public String getName() {
            return name;
        }
        public double getSalary() {
            return salary;
            
        }
        public int getId()
        {
            return id;
            
        }
        public void setId() {
            id=nextId;
            nextId++;
            
        }
        public static int getNextId() {
            return nextId;
            
        }
        public static void main(String[] args) {
            Employee e =new Employee("Harry",50000);
            System.out.println(e.getName()+" "+e.getSalary());
        }
    }

    package com.java.StaticTest;

    public class StaticTest {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Employee[] staff = new Employee[3];
            staff[0]=new Employee("Tom",40000);
            staff[1]=new Employee("Dick",60000);
            staff[2]=new Employee("Harry",65000);
            for (Employee e:staff) {
                e.setId();
                System.out.println("name="+e.getName()+",id="+e.getId()+",salary="+e.getSalary());
            }
            
            int n = Employee.getNextId();
            System.out.println("Next available id="+n);
            

        }

    方法参数

    程序设计语言中参数传递方法有两种

    一种是按值调用,一种是按引用调用

    按值调用即方法接受的是调用者提供的值,即得到一个所求的值的拷贝值,方法不能修改传递给他们的任何参数变量的内容

    按引用调用即方法节后的是调用者提供的变量地址。

    java采用的是按值调用。

    代码验证:package com.java.ParamTest;

    public class Employee {
        private String name;
        private double salary;
        
        public Employee(String n,double s) {
            name =n;
            salary=s;
            
            
        }
        public String getName() {
            return name;
            
        }
        
        public double getSalary()
        {
            return salary;
        }
        public void raiseSalary(double byPercent) {
            double raise =salary*byPercent/100;
            salary +=raise;
            
        }
    }
    package com.java.ParamTest;

    public class ParamTest {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("Testing tripleValue:");
            double percent=10;
            System.out.println("Before :percent="+percent);
            tripleValue(percent);
            System.out.println("After:percent="+percent);
            
            
            
            
            
            
            
            System.out.println(" Testing tripleSalary:");
            Employee harry=new Employee("Harry",50000);
            System.out.println("Before :salary="+harry.getSalary());
            tripleSalary(harry);
            System.out.println("After :salary="+harry.getSalary());
            
            
            
            
            
            System.out.println(" Testing swap:");
            Employee a=new Employee("Alice",70000);
            Employee b=new Employee("Bob", 60000);
            System.out.println("Before :a="+a.getName());
            System.out.println("Before :b="+b.getName());
            swap(a,b);
            System.out.println("After :a="+a.getName());
            System.out.println("After :b="+b.getName());
            

        }
        
        
     
        
        

        public static void tripleSalary(Employee x) {
            // TODO Auto-generated method stub
            x.raiseSalary(200);
            System.out.println("End of method:salary="+x.getSalary());
            
        }

        public static void tripleValue(double x) {
            // TODO Auto-generated method stub
            x=3*x;
            System.out.println("End of method:x="+x);
            
            
        }
        
        
        public static void swap(Employee x,Employee y) {
            Employee temp =x;
            x=y;
            y=temp;
            System.out.println("End of method:x="+x.getName());
            System.out.println("End of method:y="+y.getName());
        }

    }
    结果:Testing tripleValue:
    Before :percent=10.0
    End of method:x=30.0
    After:percent=10.0

    Testing tripleSalary:
    Before :salary=50000.0
    End of method:salary=150000.0
    After :salary=150000.0

    Testing swap:
    Before :a=Alice
    Before :b=Bob
    End of method:x=Bob
    End of method:y=Alice
    After :a=Alice
    After :b=Bob

    可以分析出,方法参数分为两个类型:

    基本数据类型;

    对象引用;

    其中基本数据类型完成时对原本数据的拷贝,所有操作都是对其拷贝的内容进行操作,与原数据无关。

    而对于对象引用是因为副本指向的也是new出来的地址,故会对其改变。

    而交换的也是副本,并非本身,在交换完之后,就将其丢弃。故不变。

    如果有写的不对的地方,欢迎指正,希望能够共同进步,谢谢!
  • 相关阅读:
    JS异步操作之promise发送短信验证码.html
    JS高级之简单类的定义和继承
    逮住一个bug
    基于localStorage的登录注册
    脱离node自己使用普通的requirejs管理js资源
    前端的console.log的效果写法
    第六节 全文索引
    第五节 索引
    第四节 数据的删除
    第三节 基本操作之数据--------更新
  • 原文地址:https://www.cnblogs.com/zzuzhouxiang/p/10328302.html
Copyright © 2020-2023  润新知