标签:java
<span style="font-size:18px;">public class Hello {
String s = "Hello";
public Hello(String s)
{
System.out.println("s = " + s);
System.out.println("1 -> this.s = " + this.s);
this.s = s;
System.out.println("2 -> this.s = " + this.s);
}
public static void main(String[] args)
{
Hello x="new" Hello("HelloWorld!");
}
}</span><span style="font-size:18px;">/*父类*/
class Song
{
//成员变量
private String name;
private String singer;
//构造方法
public void setInfo(String name,String singer )
{
this.name = name;
this.singer = singer;
}
//成员方法
protected void print()
{
System.out.println("Name="+name+"Singer="+singer);
}
}
/*子类*/
public class superDemo extends Song
{
//子类成员方法
public void print()
{
System.out.println("superDemo");
super.print(); //调用父类的print成员方法
}
public static void main(String[] args)
{
superDemo demo=new superDemo(); //实例化一个 superDemo对象,demo能够引用父类所有非private成员
demo.setInfo("可惜没如果","林俊杰");
demo.print();
}
}</span>
<span style="font-size:18px;">/*父类*/
class Song
{
//构造方法1
public Song()
{
System.out.println("Father class");
}
//构造方法2
public Song(String name )
{
System.out.println("Father Class"+name);
}
}
/*子类*/
public class superDemo extends Song
{
//构造方法1
superDemo()
{
super(); //super引用父类构造函数1
System.out.println("Child class --A song"+"\n"+"-------------------------");
}
//构造方法2
superDemo(String name)
{
super(name); //super引用父类构造函数2
System.out.println("Child class name is"+name+"\n"+"-------------------------");
}
//构造方法3
superDemo(String name,String singer)
{
this(name); //this引用自身构造方法2
System.out.println("Child class singger is"+singer+"\n"+"-------------------------");
}
public static void main(String[] args)
{
superDemo demo=new superDemo(); //实例化一个子类对象,构造方法为第一种
new superDemo("钟汉良"); //实例化一个子类对象,构造方法为第二种
new superDemo("钟汉良","何以笙箫默"); //实例化一个子类对象,构造方法为第三种
}
}</span>
标签:java
原文地址:http://blog.csdn.net/u012637501/article/details/43192703