学习总结 java 父子级

时间:2022-05-20 18:16:28
 package com.hanqi;

 //父类
public class Father { // public Father()
// {
//
// }
//
public Father(String name)
{
this.name = name;
System.out.println("调用了父类的构造方法");
} //名字 private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} //工作 public void work()
{
System.out.println("我是"+name+" 我劳动,我光荣!");
} private void siFangQian()
{
System.out.println("存点私房钱");
} }
 package com.hanqi;

 //子类 继承了父类的方法
public class Son extends Father{ public Son(String name)
{
//自动调用父类的构造方法
//如果父类没有默认构造方法,就要显示调用
super(name); // this代表自己 super代表上一级 System.out.println("调用了子类的构造方法");
} //唱歌
public void Sing()
{
System.out.println("喜欢唱歌");
} //重写 覆盖
@Override
public void work()
{
super.work();
//System.out.println("我不喜欢上班,我想唱歌。");
System.out.println("边上班边唱歌"); } public void work(String str)
{ }

测试

 package com.hanqi;

 public class Test14 {

     public static void main(String[] args) {

         Son s = new Son("儿子");

         //s.setName("儿子");

         s.work();

         s.Sing();
} }

学习总结  java  父子级