第九周上机
package ydy12; public class Point { int x; int y; Point() { } Point(int x0, int y0) { this.x = x0; this.y = y0; } public void movePoint(int dx, int dy) { this.x += x; this.y += y; System.out.println("坐标" + this.x + "," + this.y); } }
测试
package ydy12; public class ceshi { public static void main(String[] args) { Point p1 = new Point(1,1); Point p2 = new Point(4,5); p1.movePoint(p2.x,p2.y); } }
package com.xuexiao; class RectangleDemo{ /*定义一个矩形类Rectangle:(知识点:对象的创建和使用) 1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。 2 有2个属性:长length、宽width 3 创建一个Rectangle对象,并输出相关信息*/ private int length; private int width; RectangleDemo(int length,int width){ this.length = length; this.width = width; } public int getArea() { return length*width; } public int getPer() { return (length+width)*2; } public void showAll() { System.out.println("长:"+length+" 宽:"+width+" 面积:"+getArea()+" 周长:"+getPer()); } } public class Rectangle { public static void main(String[] args) { RectangleDemo r = new RectangleDemo(2,3); r.showAll(); } }
package bwk; public class laptop { private String color; private int cpu; public laptop(String color,int cpu) { this.color =color; this.cpu=cpu; } public void inform() { System.out.println("颜色为:"+color+" cpu型号为:i"+cpu); } public static void main(String[] args) { laptop l = new laptop("白色",7); laptop l1 = new laptop("black",5); l.inform(); l1.inform(); } } /*3、定义一个笔记本类,该类有颜色(char)和cpu 型号(int)两个属性。 [必做题] • 3.1 无参和有参的两个构造方法;有参构造方法可 以在创建对象的同时为每个属性赋值; • 3.2 输出笔记本信息的方法 • 3.3 然后编写一个测试类,测试笔记本类的各个 方法*/
package bwk; public class laptop { private String color; private int cpu; public laptop(String color,int cpu) { this.color =color; this.cpu=cpu; } public void inform() { System.out.println("颜色为:"+color+" cpu型号为:i"+cpu); } public static void main(String[] args) { laptop l = new laptop("白色",7); laptop l1 = new laptop("black",5); l.inform(); l1.inform(); } } /*3、定义一个笔记本类,该类有颜色(char)和cpu 型号(int)两个属性。 [必做题] • 3.1 无参和有参的两个构造方法;有参构造方法可 以在创建对象的同时为每个属性赋值; • 3.2 输出笔记本信息的方法 • 3.3 然后编写一个测试类,测试笔记本类的各个 方法*/
package bwk; class Person{ private String name; private int age; private float heigh; public Person(String name,int age,float heigh) { this.name=name; this.age=age; this.heigh=heigh; } public void sayHello() { System.out.println("hello,my name is "+name+" 我的年龄为"+age+" 我的身高为"+heigh+"cm"); } } public class PersonCreate { public static void main(String[] args) { // TODO Auto-generated method stub Person p =new Person("张三",33,173); Person m =new Person("李四",44,174); p.sayHello(); m.sayHello(); } } /*5、定义两个类,描述如下: [必做题] • 5.1.1定义一个方法sayHello(),可以向对方发出问 候语“hello,my name is XXX” • 5.1.2有三个属性:名字、身高 age • 5.2定义一个PersonCreate类: • 5.2.1创建两个对象,分别是zhangsan,33岁,1.73 ;lishi,44,1.74 • 5.2.2分别调用对象的sayHello()方法。*/