zoukankan      html  css  js  c++  java
  • 基类和派生类小程序--简单

    import java.util.Scanner;

    class Person

    {

      private String name;

      private int age;

      public Person()

      {

        System.out.println("调用了Person类的无参数构造方法");

      }

      public Person(String name,int age)

      {

        System.out.println("调用了Person类的有参数构造方法");

        this.name=name;

        this.age=age;

      }

      public void show()

      {

        System.out.println("姓名:"+name+"   年龄:"+age);

      }

    }

    class Student extends Person  //Student是子类

    {

      private String department;

      public Student()

      {

        System.out.println("调用了学生类的无参数构造方法Student()");

      }

      public Student(String name,int age,String dep)

      {

        super(name,age);

        department=dep;

        System.out.println("我是"+department+"的学生");

        System.out.println("调用了学生类的有参数构造方法Student(String name,int age,String dep)");

      }

    }

    public class App8_2 {

      public static void main(String[] args)

      {

        String sname,dept;

        int age;

        Student stu1=new Student();

        Scanner reader=new Scanner(System.in);

        System.out.print("请输入姓名,年龄和所在的系:");

        sname=reader.next();

        age=reader.nextInt();

        dept=reader.next();

        //Student stu2=new Student("李小四",23,"信息系");

        Student stu2=new Student(sname,age,dept);

        stu1.show();

        stu2.show();

         reader.close()

      }

    }

    运行结果:

  • 相关阅读:
    查看本机上的端口使用情况netstat -an
    WCF中的由于目标计算机积极拒绝,无法连接
    联想G480安装CentOS电缆驱动器
    "伪中国移动client"--伪基站诈骗
    Median of Two Sorted Arrays--LeetCode
    poj3671Dining Cows(DP)
    更多RANK37
    《Java并发编程实战》第二章 线程安全 札记
    Windows下一个SlikSVN使用
    (一个)AngularJS获取贴纸Hello World
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11133925.html
Copyright © 2011-2022 走看看