0基础学java_构造方法和匿名对象

构造方法的格式:

class 类名称{

   访问权限 类名称(类型1 参数1, 类型2 参数 2……){

   程序语句;

   ……  //构造方法没有返回值

}

}

  1. 构造方法的名称必须与类名称保持一致
  2. 构造方法的声明处不能有任何返回值类型的声明
  3. 不能在构造方法中使用return返回一个值

在整个java操作中,如果一个类中没有明确的声明一个构造方法,则会自动生成一个无惨的什么都不做的构造方法供用户使用。

构造方法的主要目的是为类中的属性初始化。

举例:

 1 package company.feimao.package1;
 2 class Person03{
 3     private String name;
 4     private int age;
 5     public Person03(String n , int a){ //声明构造方法,为类中的属性初始化
 6         this.setName(n);
 7         this.setAge(a);
 8     }
 9     public void setName(String n){
10         name = n;
11     }
12     public String getName(){
13         return name;
14     }
15     public void setAge(int a){
16         age = a;
17     }
18     public int getAge(){
19         return age;
20     }
21     public void tell(){
22         System.out.println("姓名:" + this.name + ", 年龄:" + this.age);
23     }
24 }
25 public class ClassDemo03 {
26     public static void main(String args[]){
27         Person03 p = new Person03("李四" , 34);
28         /*p.setName("李四");
29         p.setAge(39);*/
30         p.tell();
31 
32     }
33 }

 举例:用java程序实现一个学生类,学生的姓名、学号、数学成绩89、英语成绩98、计算机成绩93,并求三门课的成绩之和、平均值和最大值

 1 package company.feimao.package1;
 2 
 3 class Student{
 4     private String name;
 5     private String stuNo;
 6     private float math;
 7     private float english;
 8     private float coumputer;
 9     public Student(String n , String s , float m , float e , float c){
10         this.setName(n);
11         this.setStuNo(s);
12         this.setMath(m);
13         this.setEnglish(e);
14         this.setCoumputer(c);
15 
16     }
17         public void setName(String n){
18              name = n;
19         }
20         public String getName(){
21             return name;
22         }
23         public void setStuNo(String s){
24             stuNo = s;
25         }
26         public String getStuNo(){
27             return stuNo;
28         }
29         public void setMath(float m){
30             math = m;
31         }
32         public float getMath(){
33            return math;
34         }
35         public void setEnglish(float e){
36             english = e;
37         }
38         public float getEnglish(){
39             return english;
40         }
41         public void setCoumputer(float c){
42             coumputer = c;
43         }
44         public float getCoumputer(){
45             return  coumputer;
46         }
47         public float sum(){
48             return math + english + coumputer;
49         }
50         public float avg(){
51             return this.sum() / 3;
52         }
53         public float max(){
54             float max = math;
55             max = max > coumputer?max : math;
56             max = max > english?max : english;
57             return  max;
58         }
59     }
60 public class ClassDemo04 {
61     public static void main(String args[]){
62         Student s = new Student("李磊" , "XX9527" , 89.0f , 98.0f , 93.0f);
63         System.out.println("学生姓名:" + s.getName());
64         System.out.println("学生编号:" + s.getStuNo());
65         System.out.println("三门课总分:" + s.sum());
66         System.out.println("三门课平均分:" + s.avg());
67         System.out.println("三门课最高分:" + s.max());
68 
69 
70     }
71 }

 匿名对象:即没有名字的对象

1 new Person().talk();

匿名对象使用方法:

1.类中的方法仅被调用一次

2.匿名对象作实参传递

匿名对象匿名对象执行完毕后,由于再无引用引用之,Java的自动回收机制会视作垃圾处理。