师从鬼谷 2022-03-30 19:31 采纳率: 100%
浏览 36
已结题

求优化一下关于JAVA多态的问题

img

img


我看每个对象都要写一遍输出代码很麻烦,有没有一种方法可以只要写一遍,再从各个对象调用数据就好的方法。

  • 写回答

3条回答 默认 最新

  • Tomshidi 2022-03-30 21:32
    关注

    使用设计模式应该能让代码看起来更优雅,但是你不一定能理解。所以暂且就写成下面这样吧。

    public abstract class Student {
        protected int xh;
    
        protected String xm;
    
        protected char xb;
    
        protected double sg;
    
        public void information() {
            System.out.println("学号:" + xh);
            System.out.println("姓名:" + xm);
            System.out.println("性别:" + xb);
            System.out.println("身高:" + sg);
            System.out.println("--------------------");
        }
    }
    
    public class ZhangSan extends Student {
    
        public ZhangSan() {
            this.xh = 110;
            this.xm = "张三";
            this.xb = '男';
            this.sg = 1.85;
        }
    }
    
    public class LiSi extends Student {
    
        public LiSi() {
            this.xh = 120;
            this.xm = "李四";
            this.xb = '女';
            this.sg = 1.65;
        }
    }
    
    public class StartMain {
        public static void main(String[] args) {
            new ZhangSan().information();
            new LiSi().information();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
  • skp81 2022-03-30 19:41
    关注

    在父类实现 然后用调用

    评论
  • 宁-静-致-远 2022-03-30 21:03
    关注
    
    /**
     * 你写的代码的情况是一个类的多个实例
     * 只需要实例化Student赋值不同实例的参数即可实现 张三、李四。。。。
     */
    public class Student {
        private int xh;
        private String xm;
    
        public int getXh() {
            return xh;
        }
    
        public void setXh(int xh) {
            this.xh = xh;
        }
    
        public String getXm() {
            return xm;
        }
    
        public void setXm(String xm) {
            this.xm = xm;
        }
    
        public char getXb() {
            return xb;
        }
    
        public void setXb(char xb) {
            this.xb = xb;
        }
    
        public double getSg() {
            return sg;
        }
    
        public void setSg(double sg) {
            this.sg = sg;
        }
    
        private char xb;
        private double sg;
    
        public void information(){
            System.out.println("学号:" + xh);
            System.out.println("姓名:" + xm);
            System.out.println("性别:" + xb);
            System.out.println("身高:" + sg);
        }
    
        public static void main(String[] args) {
            //实例化张三, zhangsanStudent 是  Student 类实例化后的一个具体对象
            Student zhangsanStudent = new Student();
            zhangsanStudent.setXh(110);
            zhangsanStudent.setXm("张三");
            zhangsanStudent.setXb('男');
            zhangsanStudent.setSg(1.8);
            zhangsanStudent.information();
            System.out.println("------------------------------------");
            //实例化李四, lisiStudent 是  Student 类实例化后的一个具体对象
            Student lisiStudent = new Student();
            lisiStudent.setXh(120);
            lisiStudent.setXm("李四");
            lisiStudent.setXb('女');
            lisiStudent.setSg(1.65);
            lisiStudent.information();
            //.....可以实例化无数个学员对象 Student wangwuStudent = new Student();
        }
    }
    
    

    如有帮助,请采纳!!!

    关于多态讲解,可以看一下廖老师的博文
    多态 - 廖雪峰的官方网站

    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 创建了问题 3月30日

悬赏问题

  • ¥15 二分类改为多分类问题
  • ¥15 Unity微信小游戏上调用ReadPixels()方法报错
  • ¥15 如何通过求后验分布求得样本中属于两种物种其中一种的概率?
  • ¥15 q从常量变成sin函数,怎么改写python代码?
  • ¥15 图论编程问题,有可以指导的吗
  • ¥15 DEA的CCR模型画图
  • ¥15 请假各位一个关于安卓车机的问题
  • ¥15 光谱仪怎么看这样的透射谱
  • ¥15 pyqt5 如何实现输入框输入关键词,下拉框显示模糊查询返回的结果?
  • ¥20 fluent模拟,可以燃烧和相变同时模拟吗?