Java 对象示例:模拟女朋友类,包含属性和行为
// 女朋友类 public class Girlfriend { private String name; private double height; private double weight;
public Girlfriend(String name, double height, double weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
public void wash() {
System.out.println('女朋友帮我洗衣服');
}
public void cook() {
System.out.println('女朋友给我做饭');
}
public void show() {
System.out.println('我女朋友叫' + name + ',身高' + height + '厘米,体重' + weight + '斤');
}
}
// 测试类 public class Test { public static void main(String[] args) { Girlfriend gf = new Girlfriend('凤姐', 155.0, 130.0); gf.show(); // 展示属性值 gf.wash(); // 洗衣服方法 gf.cook(); // 做饭方法 } }
// 打印结果: 我女朋友叫凤姐,身高155.0厘米,体重130.0斤 女朋友帮我洗衣服 女朋友给我做饭
原文地址: https://www.cveoy.top/t/topic/oksf 著作权归作者所有。请勿转载和采集!