JAVA练习题求助

习题1:编写一个名为Parent的类,类中偶一个叫Method1()和一个Method2()的方法,方法的内容就是将方法所属的类和方法名输出到控制台。然后编写一个Child类继承Parent()类,让Child类覆盖method1()方法,方法内容也是想控制太输出方法所属的类和方法名

public class T4 {
public static void main(String[] args) {
Parent p = new Parent();
Parent c = new Child();
p.Method1();
p.Method2();
c.Method1();
}
}
class Parent{

public void Method1(){
System.out.println(this.getClass().getName());
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
}
public void Method2(){
System.out.println(this.getClass().getName());
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
}
}

class Child extends Parent{
@Override
public void Method1() {
System.out.println(this.getClass().getName());
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
}
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2015-07-07
package com.prj.test;

public class Parent {
public static void main(String[] args) {
new Parent().printMethodName();
}
public void printMethodName(){
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
}
public void printClassName(){
System.out.println(this.getClass().getName());
}
}

class Son extends Parent{
@Override
public void printClassName() {
// TODO Auto-generated method stub
super.printClassName();
}

@Override
public void printMethodName() {
// TODO Auto-generated method stub
super.printMethodName();
}
}
相似回答