问:这个程序能否编译通过?如果可以,输出结果是什么?如果不可以,则应该如何修改?
class Dog{
public String name;//定义一个name 属性,该属性为String 类型
public int age;//定义一个age 属性,该属性为int 类型
//定义一个sexual 属性,该属性为boolean 类型,true 表示为公,false 表示为母
public boolean sexual;
public Dog(){}
public Dog(String name, int age, boolean sexual){
this.name = name;
this.age = age;
this.sexual = sexual;
}
public void play(){
System.out.println(name + "play");
}
public void play(int n){
System.out.println(name + "play" + n + "minutes");
}
}
public class TestDog{
public static void main(String args[]){
/**
* 创建一个Dog 对象,利用带参数的构造函数
* 名字为joy,年龄为2 岁,性别为母
*/
Dog d = new Dog("joy",2,false);
d.play(); //调用Dog 对象无参的play 方法。
d.play(10); //调用Dog 对象有参的play 方法,参数为30
}
}
public class Student {
int age;
String name;
public Student() {}//构造函数不能有返回值
void init() {
age = 10;
name = "limy";
}
public Student(String name) {
this.init();
this.name = name;
}
public Student(String name, int age) {
this(name);//this语句放在第一行
this.init();
this.age = age;
}
}
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("我是无参的work方法");
}
public void work(int time){
System.out.println("我是带有一个参数的work方法");
}
public static void main(String[] args) {
}
}
public class Address{
public String address;//地址
public String zipCode;//邮编
public Address() {}//无参构造方法
public Address(String address, String zipCode) {
this.address = address;
this.zipCode = zipCode;
}
public static void main(String[] args) {
}
}
public class Worker {
public String name;//姓名
public int age;//年龄
public double salary;//工资
public Address addr;//地址
public Worker() {}//无参构造方法
public Worker(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public Worker(String name, int age, double salary, Address addr) {
super();
this.name = name;
this.age = age;
this.salary = salary;
this.addr = addr;
}
public void work(){
System.out.println("我是无参的work方法");
}
public void work(int time){
System.out.println("我是带有一个参数的work方法");
}
@Override
public String toString() {
//注意:addr.toString()要用到Address类中的toString()方法
return "Worker [name=" + name + ", age=" + age + ", salary=" + salary + ", " + addr.toString() + "]";
}
public static void main(String[] args) {
Worker worker = new Worker("zhangsan",25,2500,
new Address("北京市海淀区清华园1 号", "100084"));
System.out.println(worker.toString());
}
}
面向对象的理解并举例
eg:创建一个学生类,有学习、跑步相关方法
姓名、性别、年龄相关属性
要求:
1、通过封装方式实现学生信息的输出
2、归纳总结封装的优点
public class Student {
private String name;//姓名
private String sex;//性別
private int age;//年齡
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String study(){
return "我喜欢Java编程。";
}
public String play(){
return "我也喜欢锻炼身体。";
}
public static void main(String[] args) {
Student s = new Student();
s.setName("Jack");
s.setSex("Man");
s.setAge(20);
System.out.println(s.getName()+"\t"+s.getSex()+"\t"+s.getAge()+
"\t"+s.study()+"\t"+s.play());
}
}
2)封装的优点
1、将变化隔离
2、便于使用
3、提高重用性
4、提高安全性
4、列出构造函数和一般函数的区别
5、this关键字的特点以及三种应用场景,举例说明
public class Demo05 {
private String name;
private String password;
public Demo05(String name, String password) {
this();// 2、在本类中,在构造函数调用其他构造函数
this.name = name;
this.password = password;
}
public Demo05(String name) {
this.name = name;// 1、在构造方法中调用类变量信息
}
public Demo05() {
}
public void study() {
System.out.println("study");
}
public void run() {
study();// 3、调用同类中其他的函数信息,这时候this可以省略不写
System.out.println("run");
}
}
public class Person {
private int id;
private int age;
private String name;
public Person() {}
public Person(int id) {
this.id = id;
}
public Person(int id, int age) {
this(id);
this.age = age;
}
public Person(int id, int age, String name) {
this(id,age);
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void getAge(Person p1, Person p2){
if(("".equals(p1.getName().trim()) || p1.getName()==null) || ("".equals(p2.getName().trim()) || p2.getName()==null)){
System.out.println("姓名不能為空!");
}
else{
if(p1.getAge()>p2.getAge()){
System.out.println(p1.getName()+"的年齡大一些!");
}
else if(p1.getAge()==p2.getAge()){
System.out.println("年齡相等!");
}
else{
System.out.println(p2.getName()+"的年齡大一些!");
}
}
}
}