zoukankan      html  css  js  c++  java
  • Java入门——day49

    1.人、学生和老师

    • 设计一个类people,有数据成员:age(年龄,整型)name(姓名,string),行为成员:两个构造函数(一个默认,另一个有参数);void setValue(int m, string str)agename赋值;有一个void类型的抽象函数display()

    • 设计一个学生类student,公有继承类people,有私有成员:studentID(学号,整型),行为成员:两个构造函数(一个默认,另一个有参数);void setID(int m)studentID赋值;display()函数输出学生的姓名,年龄,学号。

    • 设计一个教师类teacher,公有继承类people,有私有成员:teacherID(工号,整型),行为成员:两个构造函数(一个默认,另一个有参数);void setID(int m)teacherID赋值; display()函数输出教师的姓名,年龄,工号。

    main函数定义学生对象和教师对象,给对象初始化赋值或调用setValue()setID()赋值,并输出学生和老师的信息。

     

     1 import java.util.Scanner;
     2 //父类People
     3 abstract class People{
     4     protected int age; //年龄
     5     protected String name; //姓名
     6     public People() {
     7     }
     8     public People(int age, String name) {
     9         this.age = age;
    10         this.name = name;
    11     }
    12     public void setValue(int m,String str) {
    13         age=m;
    14         name=str;
    15     }
    16     //抽象函数
    17     public abstract void display();
    18 }
    19 //子类Student继承People类
    20 class Student extends People{
    21     private int studentID;  //学号
    22     public Student() {
    23         super();
    24     }
    25     public Student(int age, String name,int studentID) {
    26         super(age, name);
    27         this.studentID=studentID;
    28     }
    29     public void setID(int m) {
    30         studentID=m;
    31     }
    32     //实现抽象类
    33     public void display() {
    34         System.out.println("姓名:"+name);
    35         System.out.println("年龄:"+age);
    36         System.out.println("学号:"+studentID);
    37     }
    38 }
    39 //子类Teacher继承People类
    40 class Teacher extends People{
    41     private int teacherID;  //工号
    42     public Teacher() {
    43         super();
    44     }
    45     public Teacher(int age, String name,int teacherID) {
    46         super(age, name);
    47         this.teacherID=teacherID;
    48     }
    49     public void setID(int m) {
    50         this.teacherID=m;
    51     }
    52     //实现抽象类
    53     public void display() {
    54         System.out.println("姓名:"+name);
    55         System.out.println("年龄:"+age);
    56         System.out.println("工号:"+teacherID);
    57     }
    58 }
    59 public class Test {
    60     public static void main(String[] args) {
    61         String str;
    62         int a,ID;
    63         Scanner in=new Scanner(System.in);
    64         Student s=new Student(18,"李明",20195555);
    65         Teacher t=new Teacher();
    66         s.display();
    67         System.out.print("请输入教师的姓名、年龄和工号:");
    68         str=in.next();
    69         a=in.nextInt();
    70         ID=in.nextInt();
    71         t.setValue(a, str);
    72         t.setID(ID);
    73         t.display();
    74     }
    75 }

     


    2.动物类

    定义猫科动物Animal类,由其派生出猫类(Cat)和豹类(Leopard),在Animal类中定义抽象函数,输出“My name is Animal”,在派生类中分别重新定义该函数,显示“My name is  **”,其中**为各自类名。

     1 //父类Animal
     2 class Animal{
     3     public void display() {
     4         System.out.println("My name is Animal");
     5     }
     6 }
     7 //子类Cat继承Animal类
     8 class Cat extends Animal{
     9     public void display() {
    10         System.out.println("My name id Cat");
    11     }
    12 }
    13 //子类Leopard继承Animal类
    14 class Leopard extends Animal{
    15     public void display() {
    16         System.out.println("My name is Leopard");
    17     }
    18 }
    19 public class Test {
    20     public static void main(String[] args) {
    21         Cat c=new Cat();
    22         c.display();
    23         Leopard l=new Leopard();
    24         l.display();
    25     }
    26 }

     

  • 相关阅读:
    window.clearInterval与window.setInterval的用法(
    hibernate 使用in方式删除数据
    hibernate中一对多Set的排序问题
    struts2 标签的使用之一 s:if(遍历中s:if如何用等)
    hibernate使用sql语句查询实体时,要写上addEntity
    struts通过Ajax返回数据时,例如对象类型,没有执行Ajax的回调函数
    hibernate 对象状态异常:object references an unsaved transient instance
    ${}与 $()区别
    hibernate逆向工程生成的实体映射需要修改
    本地tomcat的start.bat启动时访问不出现小猫图标
  • 原文地址:https://www.cnblogs.com/znjy/p/13550974.html
Copyright © 2011-2022 走看看