续java学习18:https://blog.csdn.net/qq_40790831/article/details/86484501
- 创建对象时,JVM会付一个初始数值
- 联练习:
- 、创建电饭锅(品牌,颜色,大小),汽车(品牌,颜色,排量,类型),学生(姓名,年龄,性别)等实体类;
- 、测试所有实体类;
测试类
/*
测试类
*/
public class TestClass {
public static void main ( String [] arges ) {
ElectricCooker ec = new ElectricCooker( "海尔" , "红色" , 1.5 ) ;
System.out.println( ec.getSize() + "L的" + ec.getColor() + ec.getBrand() + "牌电饭锅" ) ;
Car c = new Car( "大众" , "黑色" , 3.5 , "汽车" ) ;
System.out.println( c.getDisplacement() + "L排量的" + c.getColor() + c.getBrand() + c.getType() ) ;
Student stu = new Student("XM",18,"男") ;
System.out.println( stu.getName() + "->" + stu.getAge() + "->" + stu.getSex() ) ;
}
}
电饭锅类:
/*
电饭锅类
*/
public class ElectricCooker {
// 品牌属性
String brand ;
// 颜色属性
String color ;
// 大小属性
double size ;
// 构造函数
public ElectricCooker ( String brand , String color , double size ) {
this.brand = brand ;
this.color = color ;
this.size = size ;
}
// get方法,用于属性数据
public String getBrand() { return this.brand ; }
public String getColor() { return this.color ; }
public double getSize() { return this.size ; }
}
汽车类:
/*
汽车对象实体类
*/
public class Car {
// 品牌属性
String brand ;
// 颜色属性
String color ;
// 排量属性
double displacement ;
// 类型属性
String type ;
// 构造函数
public Car( String brand , String color , double displacement , String type ) {
this.brand = brand ;
this.color = color ;
this.displacement = displacement ;
this.type = type ;
}
// get方法,用于获取属性值
public String getBrand() { return this.brand ; }
public String getColor() { return this.color ; }
public double getDisplacement() { return this.displacement ; }
public String getType() { return this.type ; }
}
学生类:
/*
学生实体类
*/
public class Student {
// 姓名属性
String name ;
// 年龄属性
int age ;
// 性别
String sex ;
// 构造函数
public Student( String name , int age , String sex ) {
this.name = name ;
this.age = age ;
this.sex = sex ;
}
// get方法,用于属性获取
public String getName() { return this.name ; }
public int getAge() { return this.age ; }
public String getSex() { return this.sex ; }
}