zoukankan      html  css  js  c++  java
  • 类的继承,子类中输入数据,传到基类--简单

     

    import java.util.Scanner;

    class Person  //基类

    {

      private String name;

      private int age;

      String getName()

      {

        return name;

      }

      void setName(String name)

      {

        this.name=name;

      }

      int getAge()

      {

        return age;

      }

      void setAge(int age)

      {

        this.age=age;

      }

      public Person()

      {

        System.out.println("调用了个人类的构造方法。");

      }

    }

    class Student1 extends Person  //Student1是派生类

    {

      public Student1()

      {

        System.out.println("调用了学生类的构造方法。");

      }

    }

    public class exa_02 {

      public static void main(String []args)

      {

        Student1 stu=new Student1();

        Scanner reader=new Scanner(System.in);

        System.out.print("请输入学生的姓名和年龄:");

        String name1=reader.next();

        int age1=reader.nextInt();

        stu.setName(name1);

        stu.setAge(age1);

        System.out.println(stu.getName()+" ");

        System.out.println(stu.getAge()+" ");

        reader.close();

      } 

    运行结果: 

  • 相关阅读:
    UOJ388 [UNR #3] 配对树
    洛谷P6151 [集训队作业2019] 青春猪头少年不会梦到兔女郎学姐
    CF908H New Year and Boolean Bridges
    CF704C Black Widow
    CF1338
    CF1361
    CF1110H Modest Substrings
    CF1322
    CF1182F Maximum Sine
    Elastic Stack(ELK)
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11132873.html
Copyright © 2011-2022 走看看