• java 、类和对象基础题2 people


    按要求编写Java应用程序。

    1)创建一个叫做People的类:

    属性:姓名、年龄、性别、身高

    行为:说话、计算加法、改名

    编写能为所有属性赋值的构造方法;

    2)创建主类:

    创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80

    让该对象调用成员方法:

    说出“你好!”

    计算23+45的值

    将名字改为“李四”

    package lianxiti;
    
    public class People {
        private String name;
        private int age;
        private String xingbie;
        private double shengao;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getXingbie() {
            return xingbie;
        }
        public void setXingbie(String xingbie) {
            this.xingbie = xingbie;
        }
        public double getShengao() {
            return shengao;
        }
        public void setShengao(double shengao) {
            this.shengao = shengao;
        }
        People(String name,int age,String xingbie,double shengao)
        {
            this.name=name;
            this.age=age;
            this.xingbie=xingbie;
            this.shengao=shengao;
            System.out.println("姓名是:"+this.name+" "+"年龄是:"+this.age+" "+"性别是:"+
            this.xingbie+" "+"身高是:"+this.shengao+"厘米");
        }
        
        
        void ShuoHua()
        {
            System.out.println("你好");
        }
         int JiSuanJiaFa(int a,int b)
         {
            
             return a+b;
         }
         void GaiMing()
         {
             
         }
    
        
    
    }
    package lianxiti;
    
    public class p {
    
        public static void main(String[] args) {
            People p1 = new People("张三",18,"男",180.0);
            
            p1.ShuoHua();
            p1.JiSuanJiaFa(23, 45);
            System.out.println("23+45="+p1.JiSuanJiaFa(23, 45));
            p1.setName("李四");
            System.out.println(p1.getName());
            p1.setXingbie("女");
            System.out.println(p1.getName()+"的性别是"+p1.getXingbie());
    
        }
    
    }

  • 相关阅读:
    如何为惠普笔记本ProBook 4431S清理电源风扇通风口灰尘
    office app之 Mail App 从新建到发布
    sublime_text3汉化,破解,安装Package_control支持中文显示
    poj3126prime+BFS
    android开发之欢迎界面
    poj 1151 离散化
    DbVisualizer 9 解决中文乱码问题
    RS232、RS422与RS485串口标准简介
    动态创建菜单和动态关联菜单项事件
    更改路径
  • 原文地址:https://www.cnblogs.com/zhailiming/p/5511373.html
Copyright © 2020-2023  润新知