1:public String name;
public int age;
2:public boolean sexual;
3:this.name=name;
this.age=age;
this.sexual=sexual;
4:Dog d=new Dog("joy",2,"母");
5:d.play();
6:d.play(30);
①为Worker类添加三个属性:
String类型的name,表示工人的姓名
int类型的age,表示工人的年龄
double类型的salary,表示工人的工资
②为Worker类添加两个构造方法:
公开无参构造方法
接受三个参数的构造方法,三个参数分别为字符串、int和double
③为Worker类添加两个work方法:
无参方法
带有整数参数的方法,表示工人工作的时间(单位:小时)
package com.by.homework;
public class Worker {
public String name;
public int age;
public double salary;
public Worker(){
}
public Worker(String name,int age,double salary){
this.name=name;
this.age=age;
this.salary=salary;
}
public void work(){
System.out.println("这是一个误餐的方法");
}
public void workTime(int time){
System.out.println("工人的工作时间为:"+time);
}
}
Worker worker=new Worker();
worker.work();
worker.workTime(100);
Worker w=new Worker("张三",18,100);
System.out.println(w.name);
System.out.println(w.age);
System.out.println(w.salary);
①该类有两个属性:
String类型的address,表示地址
String类型的zipCode,表示邮编
②该类有两个构造方法:
无参构造方法
带有两个参数的构造方法
package com.by.homework;
public class Address {
public String address;
public String zipCode;
public Address(){
}
public Address(String a,String z){
this.address=a;
this.zipCode=z;
}
}
要求:创建工人对象,其姓名为“zhangsan”,年龄为25,工资为2500,家庭住址为“北京市海淀区清华园1号”,邮政编码为100084。
public Address addr;
public Worker(String name,int age,double salary,Address addr){
this.name=name;
this.age=age;
this.salary=salary;
this.addr=addr;
}
Worker worker=new Worker("张三",25,2500,new Address("北京市海淀区清华园1号","100084"));
System.out.println(worker.name+" "+worker.age+" "+worker.salary+" "+worker.addr.address+worker.addr.zipCode);
① 提供无参数、有参数的构造方法
② 功能方法teach(),返回值类型为void
public class Teacher {
public String tName;
public int tAge;
public boolean tSex;
public Teacher(){
}
public Teacher(String name,int age,boolean sex){
this.tName=name;
this.tSex=sex;
this.tAge=age;
}
public void teacher(){
System.out.println("这是"+tName+"的教学方法");
}
}
①提供一个无参数的构造方法和一个带有三个参数的构造方法
②类中定义一个无参数的play 方法:输出 宠物狗
③ 定义一个带有一个参数的play 方法,参数类型为 int hours,打印输出玩的小时数
④定义一个测试类,分别用无参数的构造法和有参数的构造方法创建对象,并对属性赋值,分别将对象的 信息打印在控制台上,调用play 方法
package com.by.homework;
public class Dog {
public String name;
public int age;
public boolean sex;
public Dog(){
}
public Dog(String name, int age, boolean sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void play(){
System.out.println("宠物狗");
}
public void play(int hours){
System.out.println("玩了"+hours+"个小时");
}
}
Dog d=new Dog();
d.name="小明";
d.age=2;
d.sex=true;
System.out.println(d.name);
System.out.println(d.age);
if(d.sex){
System.out.println("公");
}else {
System.out.println("母");
}
Dog dog=new Dog("小红",2,false);
System.out.println(dog.name+" "+dog.age+" "+dog.sex);
dog.play();
dog.play(10);
① 提供带有两个参数的构造方法,为用户 ID 和用户密码赋值,此时 email 采用默认的:用户名
加上”@zparkhr.com.cn”;
②提供带有三个参数的构造方法
③提供一个show 方法,用于展示用户的信息,密码处理为 xxx
④定义一个测试类,创建对象,并展示用户的信息
package com.by.homework;
public class User {
public int id;
public String password;
public String email;
public User(){
}
public User(int id, String password) {
this.id = id;
this.password = password;
}
public User(int id, String password, String email) {
this.id = id;
this.password = password;
this.email = email;
}
public void show(){
System.out.println("用户信息:"+id+Integer.parseInt(password)+email);
}
}
User user=new User(123,"1231","@zparkhr.com.cn");
System.out.println(user.id+" "+user.password+"email");
user.show();
① 提供无参数和有参数的构造方法
②编写一个测试类,创建对象并为属性赋值,将对象的信息展示在控制台上
package com.by.homework;
public class Book {
public String title;
public int pageNum;
public Book(){
}
public Book(String title, int pageNum) {
this.title = title;
this.pageNum = pageNum;
}
}
Book book=new Book("白鹿原",123);
System.out.println(book.title+book.pageNum);
① 为 Student 类添加一个 getPostAddress 方法,要求返回 Student 对象的地址和邮编
②定义一个测试类,创建对象并为属性赋值,将用户的信息进行展示
package com.by.homework;
public class Student {
public String name;
public int age;
public String address;
public String zipCode;
public int phoneNum;
public Student() {
}
public Student(String name, int age, String address, String zipCode, int phoneNum) {
this.name = name;
this.age = age;
this.address = address;
this.zipCode = zipCode;
this.phoneNum = phoneNum;
}
public Student getPostAddress(){
Student student=new Student("张三",21,"北京","@zql",123456789);
return student;
}
}
Student student=new Student();
Student s=student.getPostAddress();
System.out.println(s.address+s.zipCode);
②功能方法:移动方法(move() ),此方法一旦被调用,控制台显示”我已经启动” 展示信息 ( showInfo() ),此方法,展示商标和颜色信息
③定义一个测试类,创建一个交通工具对象,并为属性赋值;将对象的信息打印输出,并调用对象的方 法。
package com.by.homework;
public class Vehicle {
public String brand;
public String color;
public Vehicle() {
}
public Vehicle(String brand, String color) {
this.brand = brand;
this.color = color;
}
public void move(){
System.out.println("我已启动");
showInfo();
}
public void showInfo(){
System.out.println(brand+" "+color);
}
}
Vehicle v=new Vehicle();
v.brand="benz";
v.color="red";
v.move();
①提供两个构造方法,可以为属性赋值
②提供加( add() )、减( subtration() )、乘( multip() )、除( division() )功能方法
③定义一个测试类,创建该类对象,并通过调用方法完成两个数值的加、减、乘、除运算
package com.by.homework;
public class Number {
public int v1;
public int v2;
public Number() {
}
public Number(int v1, int v2) {
this.v1 = v1;
this.v2 = v2;
}
public void add(){
System.out.println("v1+v2="+v1+v2);
}
public void subtration(){
System.out.println("v1-v2="+(v1-v2));
}
public void multip(){
System.out.println("v1*v2="+(v1*v2));
}
public void division(){
System.out.println("v1/v2="+(v1/v2));
}
}
Vehicle v=new Vehicle();
v.brand="benz";
v.color="red";
v.move();
Number num=new Number(100,50);
num.add();
num.multip();
num.subtration();
num.division();
定义测试类,创建班级类对象并赋予学生数组,遍历输出学生信息
ppackage com.by.homework;
public class Clazz {
public Student student;
public Clazz() {
}
}
package com.by.homework;
public class Student {
public String name;
public int age;
public String address;
public String zipCode;
public int phoneNum;
public Student() {
}
public Student(String name, int age, String address, String zipCode, int phoneNum) {
this.name = name;
this.age = age;
this.address = address;
this.zipCode = zipCode;
this.phoneNum = phoneNum;
}
public Student getPostAddress(){
Student student=new Student("张三",21,"北京","@zql",123456789);
return student;
}
}
Clazz c=new Clazz();
Student st=new Student();
st.name="zhangsan";
st.age=18;
st.address="beijing";
st.zipCode="#123";
st.phoneNum=123435;
c.student=st;
System.out.println(c.student.name);
System.out.println(c.student.age);
System.out.println(c.student.address);
System.out.println(c.student.zipCode);
System.out.println(c.student.phoneNum);