JAVA核心技术I---JAVA基础知识(对象与类)

一:规范

将每一个类存在一个单独的源文件中

Employee.java

public class Employee {
    private int age;
    private float salay;
    private boolean sex;
    private String name;    //char name[]可以
    
    public Employee(String nm,float sy,int ag,boolean sx) {
        this.age=ag;
        this.salay=sy;
        this.sex=sx;
        this.name=nm;
    }
    
    public void printInfo() {
        System.out.println(name+" "+age+" "+salay+" "+sex);
    }
}
Employee.java

EmployeeTest.java

public class EmployeeTest {
    public static void main(String args[]) {
        Employee[] staff=new Employee[3];    //java中没有指针,可以使用数组代替
        staff[0]=new Employee("faffe",12.34f,25,true);
        staff[1]=new Employee("faaaf",12.64f,22,false);
        staff[2]=new Employee("fafwf",11.34f,26,true);
        
        for(Employee e:staff) {
            e.printInfo();
        }
    }
}
EmployeeTest.java
faffe 25 12.34 true
faaaf 22 12.64 false
fafwf 26 11.34 true

二:构造方法(同C++)

public class Employee {
    private int age;
    private float salay;
    private boolean sex;
    private String name;    //char name[]可以
    
  //多种构造,属于重载overload,不限于构造方法 public Employee() { } public Employee(Employee e) { this.age=e.getAge()+11; this.sex = !e.getSex(); this.name=e.getName(); this.salay=e.getSalay()+50; } public Employee(String nm,float sy,int ag,boolean sx) { this.age=ag; this.salay=sy; this.sex=sx; this.name=nm; }
   
    public int getAge() {
        return age;
    }
    
    public float getSalay() {
        return salay;
    }
    
    public boolean getSex() {
        return sex;
    }
    
    public String getName() {
        return name;
    }
    
    public void printInfo() {
        System.out.println(name+" "+age+" "+salay+" "+sex);
    }
View Code
}
    public static void main(String args[]) {
        Employee[] staff=new Employee[3];    //java中没有指针,可以使用数组代替
        staff[0]=new Employee("faffe",12.34f,25,true);
        staff[1]=new Employee("faaaf",12.64f,22,false);
        staff[2]=new Employee("fafwf",11.34f,26,true);
        
        for(Employee e:staff) {
            e.printInfo();
        }
        
        Employee cstaff=new Employee(staff[0]);
        cstaff.printInfo();
    }
faffe 25 12.34 true
faaaf 22 12.64 false
fafwf 26 11.34 true
faffe 36 62.34 false

不同之处:在于实例化对象时

java必须使用new实例化对象,而C++可以直接构造
C++:
Employee e("ffaw",...);
JAVA:
Employee e=new Employee("fwaf",...)

此外:java含有垃圾回收机制,所以没有析构函数。但是含有finalize方法,用于在回收对象前调用

三:main方法

每一个类都可以在单独文件中包含一个
public static void main(String args[])方法
用于测试每个模块

四:赋值问题:基本型别赋值是拷贝赋值对象赋值是reference

可以认为:基本类型的变量值小,可以直接拷贝;对象包含多个值,不容易复制,赋值采用共享同一块内存区域更加快

值传递:基本类型

        int a,b;
        a=5;
        b=a;
        System.out.println(a+" "+b);
        b++;
        System.out.println(a+" "+b);
5 5
5 6
    public static void swap(int a,int b) {
        int temp=a;
        a=b;
        b=a;
    }
        System.out.println(a+" "+b);
        swap(a,b);
        System.out.println(a+" "+b);
5 6
5 6

引用传递:对象引用

public class EmployeeTest {
    public static void main(String args[]) {
        Employee e=new Employee("fafwa",12.33f,25,true);
        Employee d=e;
        e.printInfo();
        d.printInfo();
        change(e);  //修改e,会发现d也会变化
        e.printInfo();
        d.printInfo();
    }
    
    public static void change(Employee obj) {
        obj.setAge(obj.getAge()+10);
    }
    
}
fafwa 25 12.33 true
fafwa 25 12.33 true
fafwa 35 12.33 true
fafwa 35 12.33 true

五:初值问题

对于函数局部变量,必须赋初值,不然无法通过编译。
对于类的成员变量,类似于c中的静态变量或者全局变量会被赋初值
public class EmployeeTest {
    public static void main(String args[]) {
        Employee e=new Employee();
        e.printInfo();
    }
}
public class Employee {
    private int age;
    private float salay;
    private boolean sex;
    private String name;    //char name[]可以
    
    public Employee() {
        //并未赋值,成员变量存放默认值
    }
}
null 0 0.0 false

默认初值:

– short 0 int 0 long 0L
– boolean false
– char '\u0000‘ 
– byte 0
– float 0.0f
– double 0.0d
– String null

六:类中初始化块

只要构造类的对象,就会立刻被执行
public class Employee {
    //实例域初始化块
    private int age;
    private float salay;
    private boolean sex;
    private String name;    //char name[]可以
    
    static
    {
        //....    静态初始化块,会在对象构造前执行
        System.out.println("static block");
    }
    
    {    //对象初始化块
        System.out.println("object block");
    }
    
    public Employee() {
        System.out.println("construct block");
    }
}
    public static void main(String args[]) {
        Employee e=new Employee();
        Employee d=new Employee();
    }
static block  //先执行类静态块,执行一次
object block  //每一次对象构造前都会先执行对象初始化块,放在构造方法后面也是先执行与构造方法
construct block  //之后执行构造方法
object block
construct block
    {    //对象初始化块
        System.out.println("object block");
    }
    
    public Employee() {
        System.out.println("construct block");
    }
    
    {    //对象初始化块
        System.out.println("object2 block");
    }


static block
object block
object2 block
construct block
object block
object2 block
construct block
View Code
posted @ 2018-12-28 13:21  山上有风景  阅读(498)  评论(0编辑  收藏  举报