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()); } }

本文通过一个具体的Java类实例,详细介绍了面向对象编程的基本概念,包括封装、继承和多态等特性。通过创建一个People类,演示了如何定义类属性、方法以及构造函数,展示了如何使用getter和setter方法来操作类的私有属性,并通过实例化对象进行了一系列的操作,如修改名字、说话和加法运算,最后输出对象的状态信息。

被折叠的 条评论
为什么被折叠?



