码迷,mamicode.com
首页 > 其他好文 > 详细

(1)创建一个叫做People的类: 属性:姓名、年龄、性别、身高 行为:说话、计算加法、改名 编写能为所有属性赋值的构造方法; (2)创建主类: 创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80; 让该对象调用成员方法: 说出“你好!” 计算23+45的值 将名字改为“李四”

时间:2016-05-20 21:05:29      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

package a;

public class People {
    private String name,sex;
    private int age;
    private double height;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }

    public void shuohua(String s)
    {
        System.out.println(s);
    }
    public int add(int a,int b)
    {
        int c=a+b;
        System.out.println(a+"+"+b+"="+c);
        return     c;
    }
    public String changeName(String i)
    {
        
        name=i;
        return name;
        
    }
    public static void main(String[] args) {
        People people1=new People();
        people1.setName("张三");
        people1.setSex("男");
        people1.setAge(18);
        people1.setHeight(1.80);
        System.out.println("姓名: "+people1.getName());
        System.out.println("性别: "+people1.getSex());
        System.out.println("年龄: "+people1.getAge());
        System.out.println("身高: "+people1.getHeight());
        people1.shuohua("你好");
        people1.add(23, 45);
        people1.changeName("李四");
        System.out.println("姓名: "+people1.getName());
        System.out.println("性别: "+people1.getSex());
        System.out.println("年龄: "+people1.getAge());
        System.out.println("身高: "+people1.getHeight());
        
    }
}

技术分享

(1)创建一个叫做People的类: 属性:姓名、年龄、性别、身高 行为:说话、计算加法、改名 编写能为所有属性赋值的构造方法; (2)创建主类: 创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80; 让该对象调用成员方法: 说出“你好!” 计算23+45的值 将名字改为“李四”

标签:

原文地址:http://www.cnblogs.com/wenwen123/p/5513413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!