A:案例演示 模仿学生类,完成手机类代码class Demo5_Phone { public static void main(String[] args) { Phone p1 = new Phone(); p1.setBrand("苹果"); p1.setPRice(1500); System.out.println(p1.getBrand() + "..." + p1.getPrice()); Phone p2 = new Phone("小米",98); p2.show(); }}/*手机类: 成员变量: 品牌brand,价格price 构造方法 无参,有参 成员方法 setXxx和getXxx show*/class Phone { private String brand; //品牌 private int price; //价格 public Phone(){} //空参构造 public Phone(String brand,int price) { //有参构造 this.brand = brand; this.price = price; } public void setBrand(String brand) { //设置品牌 this.brand = brand; } public String getBrand() { //获取品牌 return brand; } public void setPrice(int price) { //设置价格 this.price = price; } public int getPrice() { //获取价格 return price; } public void show() { System.out.println(brand + "..." + price); }}
07.06_面向对象(创建一个对象的步骤)(掌握)
A:画图演示 画图说明一个对象的创建过程做了哪些事情?Student s = new Student();1,Student.class加载进内存2,声明一个Student类型引用s3,在堆内存创建对象,4,给对象中属性默认初始化值5,属性进行显示初始化6,构造方法进栈,对对象中的属性赋值,构造方法弹栈7,将对象的地址值赋值给s
07.07_面向对象(长方形案例练习)(掌握)
A:案例演示 需求: 定义一个长方形类,定义 求周长和面积的方法,然后定义一个测试类进行测试。class Test1_Rectangle { //Rectangle矩形 public static void main(String[] args) { Rectangle r = new Rectangle(10,20); System.out.println(r.getLength()); //周长 System.out.println(r.getArea()); //面积 }}/** A:案例演示 * 需求: * 定义一个长方形类,定义 求周长和面积的方法, * 然后定义一个测试类进行测试。 分析: 成员变量: 宽width,高high 空参有参构造 成员方法: setXxx和getXxx 求周长:getLength() 求面积:getArea()*/class Rectangle { private int width; //宽 private int high; //高 public Rectangle(){} //空参构造 public Rectangle(int width,int high) { this.width = width; //有参构造 this.high = high; } public void setWidth(int width) {//设置宽 this.width = width; } public int getWidth() { //获取宽 return width; } public void setHigh(int high) { //设置高 this.high = high; } public int getHigh() { //获取高 return high; } public int getLength() { //获取周长 return 2 * (width + high); } public int getArea() { //获取面积 return width * high; }}
07.08_面向对象(员工类案例练习)(掌握)
A:案例演示 需求:定义一个员工类Employee自己分析出几个成员,然后给出成员变量 姓名name,工号id,工资salary 构造方法, 空参和有参的getXxx()setXxx()方法,以及一个显示所有成员信息的方法。并测试。 work class Test2_Employee { //employee员工 public static void main(String[] args) { Employee e = new Employee("令狐冲","9527",20000); e.work(); }}/** A:案例演示 * 需求:定义一个员工类Employee * 自己分析出几个成员,然后给出成员变量 * 姓名name,工号id,工资salary * 构造方法, * 空参和有参的 * getXxx()setXxx()方法, * 以及一个显示所有成员信息的方法。并测试。 * work */class Employee { private String name; //姓名 private String id; //工号 private double salary; //工资 public Employee() {} //空参构造 public Employee(String name, String id, double salary) {//有参构造 this.name = name; this.id = id; this.salary = salary; } public void setName(String name) { //设置姓名 this.name = name; } public String getName() { //获取姓名 return name; } public void setId(String id) { //设置id this.id = id; } public String getId() { //获取id return id; } public void setSalary(double salary) { //设置工资 this.salary = salary; } public double getSalary() { //获取工资 return salary; } public void work() { System.out.println("我的姓名是:" + name + ",我的工号是:" + id + ",我的工资是:" + salary + ",我的工作内容是敲代码"); }}