标签:
类名 对象名 = new 类名();
如:
1 JFrame jf = new JFrame();
1 public void study(){}
1 public class Teacher {
2 String name;
3
4 //构造方法,构造对象的时候执行
5 public Teacher(){
6 System.out.println("执行了老师类的构造方法");
7 }
8 public Teacher(String n){
9 name = n;
10 }
11
12 //方法重载
13 public void teach(int a){
14 System.out.println("执行教学方法1");
15 }
16 public void teach(){
17 System.out.println("执行教学方法2");
18 }
19 public void teach(String n){
20 System.out.println("执行教学方法3");
21 }
22 public void teach(String n,int a){
23 System.out.println("执行教学方法4");
24 }
25 public void teach(int a,String n){
26 System.out.println("执行教学方法2");
27 }
28 }
1 import javax.swing.JFrame;
2
3 public class Test {
4 public static void main(String[] args) {
5 //创建对象,调用构造方法
6 Teacher tea = new Teacher("李四");
7
8
9 Teacher tea1 = new Teacher();
10 tea.name = "张三";
11 tea1.name = "李四";
12 tea = tea1;
13 tea1.name = "王五";
14
15 System.out.println(tea.name);
16 System.out.println(tea1.name);
17
18 }
19 }
1 Teacher tea = new Teacher("李四");
上面可改写为:
1 Teacher tea;
2 tea = new Teacher("李四");
此处构造器“Teacher”只调用一次,及“执行了老师类的构造方法”只打印一次。
1 public class Student {
2 public String name;
3 public int age;
4
5 public void study(){
6 System.out.println("学生学习");
7 }
8
9 }
1 package com.huaxin.lesson0304;
2
3 public class UnStudent extends Student{
4
5 }
标签:
原文地址:http://www.cnblogs.com/xiaofu007/p/5243583.html