首页 > 编程语言 > 详细

java核心技术 要点笔记2

时间:2015-11-04 22:43:06      阅读:301      评论:0      收藏:0      [点我收藏+]

第4章   对象和类

1.面向对象

类:构造对象的模板,创建类的实例:由类构造对象的过程,封装,继承;

对象:对象的特性——对象的行为,对象的状态,对象的标识;

类之间的关系:

  依赖(“user-a”),一个类的方法操纵另一个类的对象

  聚合(“has-a”),类A的对象包含类B的对象

  继承(“is-a”),类A 拓展了类B

 

2.使用预定义类

并不是所有的类都具有面向对象的特征,例如Math类;

对象不会自动被初始化为null,而必须通过调用new或将它们设置为null进行初始化(可以将java对象看成是c++的对象指针);

 

 

3.用户自定义类

主力类:类中没有main方法,有自定义的实例域和实例方法;

在一个源文件中,只能有一个共有类,但可以有任意数目的非公有类;

多个源文件的使用: 编若两个类单独存在于两个源文件,则编译   javac Employee*.java  或者  javac EmployeeTest.java (会自动搜索Employee.java)

构造器:    构造器与类同名,  每个类可以有一个以上的构造器,   构造器可以有0个,1个或多参数,  构造器没有返回值,  伴随new操作一起使用;

隐式参数和显式参数: 关键字this表示隐式参数;import java.util.*;

 1 public class StaticTest
 2 {
 3    public static void main(String[] args)
 4    {
 5       // fill the staff array with three Employee objects
 6       Employee[] staff = new Employee[3];
 7 
 8       staff[0] = new Employee("Tom", 40000);
 9       staff[1] = new Employee("Dick", 60000);
10       staff[2] = new Employee("Harry", 65000);
11 
12       // print out information about all Employee objects
13       for (Employee e : staff)
14       {
15          e.setId();
16          System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
17                + e.getSalary());
18       }
19 
20       int n = Employee.getNextId(); // calls static method
21       System.out.println("Next available id=" + n);
22    }
23 }
24 
25 class Employee
26 {
27    private String name;
28    private double salary;
29    private int id;
30    private static int nextId = 1;
31    public Employee(String n, double s)
32    {
33       name = n;
34       salary = s;
35       id = 0;
36    }
37 
38    public String getName()
39    {
40       return name;
41    }
42 
43    public double getSalary()
44    {
45       return salary;
46    }
47 
48    public int getId()
49    {
50       return id;
51    }
52 
53    public void setId()
54    {
55       id = nextId; // set id to next available id
56       nextId++;
57    }
58 
59    public static int getNextId()
60    {
61       return nextId; // returns static field
62    }
63 
64    public static void main(String[] args) // unit test
65    {
66       Employee e = new Employee("Harry", 50000);
67       System.out.println(e.getName() + " " + e.getSalary());
68    }
69 
70 
71 }

 

 

4.静态域和静态方法

静态域:若将域定义为static,每个类中只有一个这样的域。而每个对象对于所有的实例域都有一份自己的拷贝;

静态方法:静态方法是一种不能向对象实施操作的方法,可以访问自身类中的静态域。

使用静态方法的情形:

     1、一个方法不需要访问对象状态,其所有参数都通过显示参数提供。如Math.pow(x,a)

     2、一个方法只需要访问静态域。

main方法:main方法也是一个静态方法;

每一个类都可以有一个main方法,进行单元测试; 此例中,java Employee 进行独立测试, java  StaticTest,则Employee类中的main方法不会被执行

 

 

5.方法参数

 java采用按值传输;

一个方法不能修改一个基本数据类型的参数(即数值型和布尔型);

一个方法可以改变一个对象参数的状态;

一个方法不能让对象参数引用一个新的对象;

 1 public class ParamTest
 2 {
 3    public static void main(String[] args)
 4    {
 5       /*
 6        * Test 1: Methods can‘t modify numeric parameters
 7        */
 8       System.out.println("Testing tripleValue:");
 9       double percent = 10;
10       System.out.println("Before: percent=" + percent);
11       tripleValue(percent);
12       System.out.println("After: percent=" + percent);
13 
14       /*
15        * Test 2: Methods can change the state of object parameters
16        */
17       System.out.println("\nTesting tripleSalary:");
18       Employee harry = new Employee("Harry", 50000);
19       System.out.println("Before: salary=" + harry.getSalary());
20       tripleSalary(harry);
21       System.out.println("After: salary=" + harry.getSalary());
22 
23       /*
24        * Test 3: Methods can‘t attach new objects to object parameters
25        */
26       System.out.println("\nTesting swap:");
27       Employee a = new Employee("Alice", 70000);
28       Employee b = new Employee("Bob", 60000);
29       System.out.println("Before: a=" + a.getName());
30       System.out.println("Before: b=" + b.getName());
31       swap(a, b);
32       System.out.println("After: a=" + a.getName());
33       System.out.println("After: b=" + b.getName());
34    }
35 
36    public static void tripleValue(double x) // doesn‘t work
37    {
38       x = 3 * x;
39       System.out.println("End of method: x=" + x);
40    }
41 
42    public static void tripleSalary(Employee x) // works
43    {
44       x.raiseSalary(200);
45       System.out.println("End of method: salary=" + x.getSalary());
46    }
47 
48    public static void swap(Employee x, Employee y)
49    {
50       Employee temp = x;
51       x = y;
52       y = temp;
53       System.out.println("End of method: x=" + x.getName());
54       System.out.println("End of method: y=" + y.getName());
55    }
56 }
57 
58 class Employee // simplified Employee class
59 {
60    private String name;
61    private double salary;
62 
63    public Employee(String n, double s)
64    {
65       name = n;
66       salary = s;
67    }
68 
69    public String getName()
70    {
71       return name;
72    }
73 
74    public double getSalary()
75    {
76       return salary;
77    }
78 
79    public void raiseSalary(double byPercent)
80    {
81       double raise = salary * byPercent / 100;
82       salary += raise;
83    }
84 
85 
86 }

技术分享

 

 

 6.对象的构造

重载:方法签名包括(方法名,参数类型,但是不包括返回类型),java允许重载任何方法,而不只是构造器方法;

默认域参数

 1 import java.util.*;
 2 
 3 /**
 4  * 重载构造器
 5  * this(...)调用另一个构造器
 6  * 无参数构造器
 7  *对象初始化块
 8  *静态初始化块
 9  *实例域初始化块
10  */
11 public class ConstructorTest
12 {
13    public static void main(String[] args)
14    {
15       // fill the staff array with three Employee objects
16       Employee[] staff = new Employee[3];
17 
18       staff[0] = new Employee("Harry", 40000);
19       staff[1] = new Employee(60000);
20       staff[2] = new Employee();
21 
22       // print out information about all Employee objects
23       for (Employee e : staff)
24          System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
25                + e.getSalary());
26    }
27 }
28 
29 class Employee
30 {
31 
32    private static int nextId;
33 
34    private int id;
35    private String name = ""; // instance field initialization
36    private double salary;
37 
38    // static initialization block
39    static
40    {
41       Random generator = new Random();
42       // set nextId to a random number between 0 and 9999
43       nextId = generator.nextInt(10000);
44    }
45 
46    // object initialization block
47    {
48       id = nextId;
49       nextId++;
50    }
51 
52    // three overloaded constructors
53    public Employee(String n, double s)
54    {
55       name = n;
56       salary = s;
57    }
58 
59    public Employee(double s)
60    {
61       // calls the Employee(String, double) constructor
62       this("Employee #" + nextId, s);
63    }
64 
65    // the default constructor
66    public Employee()
67    {
68       // name initialized to ""--see below
69       // salary not explicitly set--initialized to 0
70       // id initialized in initialization block
71    }
72 
73    public String getName()
74    {
75       return name;
76    }
77 
78    public double getSalary()
79    {
80       return salary;
81    }
82 
83    public int getId()
84    {
85       return id;
86    }
87 
88 }

 

 

7.静态域和静态方法

8.文档注释

9.方法参数

10.类设计技巧

java核心技术 要点笔记2

原文:http://www.cnblogs.com/zxqstrong/p/4937438.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!