zoukankan      html  css  js  c++  java
  • 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());
    
        }
    
    }

  • 相关阅读:
    c_str()函数
    Linux创建用户
    Linux vim替换命令
    计算器表达式求值源码
    结构体、共用体。面试必用
    Ubuntu打开中文输入法
    Unable to establish SSL connection
    变量的命名和使用
    C++ ARX二次开发-MFC 非模态对话框modaless dialog
    C++ ARX二次开发-创建三维实体
  • 原文地址:https://www.cnblogs.com/zhailiming/p/5511373.html
Copyright © 2011-2022 走看看