标签:
package org.leizhimin;
public class Test6 {
private int number;
private String username;
private String password;
private int x = 100;
public Test6(int n) {
number = n; // 这个还可以写为: this.number=n;
}
public Test6(int i, String username, String password) {
// 成员变量和参数同名,成员变量被屏蔽,用"this.成员变量"的方式访问成员变量.
this.username = username;
this.password = password;
}
// 默认不带参数的构造方法
public Test6() {
this(0, "未知", "空"); // 通过this调用另一个构造方法
}
public Test6(String name) {
this(1, name, "空"); // 通过this调用另一个构造方法
}
public static void main(String args[]) {
Test6 t1 = new Test6();
Test6 t2 = new Test6("游客");
t1.outinfo(t1);
t2.outinfo(t2);
}
private void outinfo(Test6 t) {
System.out.println("-----------");
System.out.println(t.number);
System.out.println(t.username);
System.out.println(t.password);
f(); // 这个可以写为: this.f();
}
private void f() {
// 局部变量与成员变量同名,成员变量被屏蔽,用"this.成员变量"的方式访问成员变量.
int x;
x = this.x++;
System.out.println(x);
System.out.println(this.x);
}
//返回当前实例的引用
private Test6 getSelf() {
return this;
}
}
package org.leizhimin;
public class Father {
public String v="Father";
public String x="输出了Father类的public成员变量x!!!";
public Father() {
System.out.println("Father构造方法被调用!");
}
public Father(String v){
this.v="Father类的带参数构造方法!运行了.";
}
public void outinfo(){
System.out.println("Father的outinfo方法被调用");
}
public static void main(String[] args) {
// TODO 自动生成方法存根
}
}
package org.leizhimin;
public class Son extends Father{
public String v="Son";
public Son() {
super(); //调用超类的构造方法,只能放到第一行.
System.out.println("Son无参数构造方法被调用!");
//super(); //错误的,必须放到构造方法体的最前面.
}
public Son(String str){
super(str);
System.out.println("Son带参数构造方法被调用!");
}
//覆盖了超类成员方法outinfo()
public void outinfo(){
System.out.println("Son的outinfo()方法被调用");
}
public void test(){
String v="哈哈哈哈!"; //局部变量v覆盖了成员变量v和超类变量v
System.out.println("------1-----");
System.out.println(v); //输出局部变量v
System.out.println(this.v); //输出(子类)成员变量v
System.out.println(super.v); //输出超类成员变量v
System.out.println("------2-----");
System.out.println(x); //输出超类成员变量v,子类继承而来
System.out.println(super.x); //输出超类成员变量v
System.out.println("------3-----");
outinfo(); //调用子类的outinfo()方法
this.outinfo(); //调用子类的outinfo()方法
super.outinfo(); //调用父类的outinfo()方法
}
public static void main(String[] args) {
new Son().test();
}
}
标签:
原文地址:http://www.cnblogs.com/AceIsSunshineRain/p/5056848.html