① 属性私有,提供公开的get、set方法
② 提供无参数、有参数的构造方法
③ 功能方法teach(),返回值类型为void
package packageload;
public class Teacher {
private String name;
private int age;
private boolean sex;
public Teacher() {
}
public Teacher(String name, int age, boolean sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public void teach(){
System.out.println("这是一个教书的方法");
}
}
public void setSex(boolean sex) {
this.sex = sex;
}
public void teach(){
System.out.println("这是一个教书的方法");
}
}
①对类进行封装,私有化属性,并提供公开的get/set方法,设置 email 采用格式为:用户名加 上”@zparkhr.com.cn”; 查看密码处理为 xxx
②提供带有两个参数的构造方法,为用户 ID 和用户密码赋值,同时提供带有三个参数的构造方法
③定义一个测试类,分别利用不同的构造方法创建对象,并利用方法,展示用户的信息。
package packageload;
public class User {
private String id;
private String password;
private String email;
public User() {
}
public User(String id, String password) {
this.id = id;
this.password = password;
this.password="xxx";
email=id+"@zparkhr.com.cn";
}
public User(String id, String password, String email) {
this.id = id;
this.password = password;
this.password="xxx";
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package packageload;
public class Test {
public static void main(String[]args){
User s=new User("123445","a123123");
System.out.println(s.getId());
System.out.println(s.getPassword());
System.out.println(s.getEmail());
User s1=new User();
s1.setId("asdasd");
s1.setPassword("xxx");
s1.setEmail("asdasd");
System.out.println(s1.getId());
System.out.println(s1.getPassword());
System.out.println(s1.getEmail());
User s2=new User("asdas","asd","asdasd");
System.out.println(s2.getId());
System.out.println(s2.getPassword());
System.out.println(s2.getEmail());
}
}
①对类进行封装,属性私有化,并提供公开的 get/set 方法;其中要求页数不能少于 200 页,否
则输出”错误信息”,并赋予默认值 200
②提供无参数和有参数的构造方法
③ 编写一个测试类,创建对象并为属性赋值,将对象的信息展示在控制台上
package packageload;
public class Book {
private String title;
private int pageNum=200;
public Book() {
}
public Book(String title, int pageNum) {
this.title = title;
this.pageNum = pageNum;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
}
Book b=new Book();
b.getPageNum();
b.setPageNum(100);
int num=b.getPageNum();
if(num<200){
System.out.println("错误信息");
}else{
System.out.println(num);
}
①将 Student 类进行封装,即属性均私有化,并提供 get/set 方法
②为 Student 类添加一个 getPostAddress 方法,要求返回 Student 对象的地址和邮编
③ 定义一个测试类,创建对象并为属性赋值,将用户的信息进行展示
package packageload;
public class Student {
private String name;
private int age;
private String address;
private String zipCode;
private 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 void getPostAddress(){
// Student student=new Student("张三",21,"北京","@zql",123456789);
this.getAddress();
System.out.println(this.address);
this.getZipCode();
System.out.println(this.zipCode);
// return student;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public int getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(int phoneNum) {
this.phoneNum = phoneNum;
}
}
Student st=new Student("张三",18,"北京天安门","000001",1234567888);
st.getPostAddress();
System.out.println(st.getName()+" "+st.getAge()+" "+st.getAddress()+" "+st.getZipCode()+" "+st.getPhoneNum());
①对类进行封装,私有化属性,并提供公开的get/set方法
②提供一个无参数的构造方法和一个带有三个参数的构造方法
③类中定义一个无参数的play 方法:输出 宠物狗
④类中定义一个带有一个参数的play 方法,参数类型为 int hours,打印输出玩的小时数
⑤定义一个测试类,分别用无参数的构造法和有参数的构造方法创建对象,并对属性赋值,分别将对象 的信息打印在控制台上,调用play 方法。
package packageload;
public class Dog {
private String name;
private int age;
private boolean sex;
public Dog(){
}
public Dog(String name, int age, boolean sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public void play(){
System.out.println("宠物狗");
}
public void play(int hours){
System.out.println("玩了"+hours+"个小时");
}
}
Dog d=new Dog("大黄",2,true);
boolean defult=d.isSex();
if(defult){
System.out.println("公狗");
}else {
System.out.println("母狗");
}
System.out.println(d.getName()+d.getAge());
d.play();
d.play(10);
①对类进行封装,私有化属性,并提供公开的get/set方法
②提供一个无参数的构造方法和有参数的构造方法
③功能方法:移动方法(move() ),此方法一旦被调用,控制台显示”我已经启动” 展示信息 ( showInfo() ),此方法,展示商标和颜色信息
④定义一个测试类,创建一个交通工具对象,并为属性赋值;将对象的信息打印输出,并调用对象的方法。
package packageload;
public class Vehicle {
private String brand;
private String color;
public Vehicle() {
}
public Vehicle(String brand, String color) {
this.brand = brand;
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void move(){
System.out.println("我已启动");
showInfo();
}
public void showInfo(){
System.out.println(brand+" "+color);
}
}
①对类进行封装,私有化属性,并提供公开的get/set方法
②提供两个构造方法,可以为属性赋值
③提供加( add() )、减( subtration() )、乘( multip() )、除( division() )功能方法
④定义一个测试类,创建该类对象,并通过调用方法完成两个数值的加、减、乘、除运算
package packageload;
public class Number {
private int v1;
private int v2;
public Number() {
}
public Number(int v1, int v2) {
this.v1 = v1;
this.v2 = v2;
}
public int getV1() {
return v1;
}
public void setV1(int v1) {
this.v1 = v1;
}
public int getV2() {
return v2;
}
public void setV2(int v2) {
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));
}
}
定义一个Worker 类,类中定义三个属性:姓名、年龄、工资(salary),要求如下:
① 对类进行封装,私有化属性,并提供公开的get/set方法
②提供公开无参构造方法和三个参数的构造方法
③定义无参的 work 方法和带整数参数 work(int hours)方法,参数代表工人工作的小时数
定义一个Address 类,类中定义属性:地址(addr)、邮编(zipCode),要求如下:
① 对类进行封装,私有化属性,并提供公开的get/set方法
②为此类提供无参数和有参数的构造方法。
③为Worker 类添加一个Address 类型的属性 address,封装并提供get/set方法
④定义一个测试类,创建一个 Worker 对象,其姓名为张三、年龄为 18、工资为 1000.0; 家庭地址为:北京市海淀区清华园 1 号院、邮编为 100010,并展示用户信息
package packageload;
public class Worker {
private String name;
private int age;
private double salary;
private packageload.Address addr;
public Worker(){
}
public Worker(String name, int age, double salary, Address addr){
this.name=name;
this.age=age;
this.salary=salary;
this.addr=addr;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
this.addr = addr;
}
public void work(){
System.out.println("这是一个无参的方法");
}
public void workTime(int time){
System.out.println("工人的工作时间为:"+time);
}
}
package packageload;
public class Address {
private String address;
private String zipCode;
private Address(String s){
}
public Address(String a,String z){
this.address=a;
this.zipCode=z;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
}
Worker worker=new Worker();
worker.setName("张三");
worker.setAge(18);
worker.setSalary(100000);
worker.setAddr(new Address("北京市海淀区清华园 1 号院","100010"));
System.out.println(worker.getName()+ worker.getAge()+ worker.getSalary()+ worker.getAddr());