(1)定义一个名字为 Father 的类,该类具有属性: age (私有)、 name (公有);具有功能: work (公有)、 drive (公有);然后,定义带参数的构造方法,对 age 和 name 进行赋值。
(2)定义主类 TestFather ,在主类的 main 方法中测试类 Father ,即:创建并初始化 Father 类的对象,输出对象的所有属性,调用对象的所有方法。
class Father {
private int age;
public String name;
public String work;
public String drive;
public Father(int age,String name){
this.age=age;
this.name=name;
}
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 String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
public String getDrive() {
return drive;
}
public void setDrive(String drive) {
this.drive = drive;
}
}
public class TestFather{
public static void main(String[] args) {
Father father=new Father(35,"sinJack");
System.out.println(father.getAge());
System.out.println(father.getName());
}
}
class Father{
private int age;
public String name;
public void word()
{
System.out.print(" 他正在上班");
}
public void drve()
{
System.out.print(" 他正在休息");
}
public Father(int age,String name)
{
this.age=age;
this.name=name;
}
public void print()
{
System.out.print("姓名:"+name+" 年龄:"+age+" ");
}
}
public class TestFather{
public static void main(String[]args)
{
Father f1=new Father(19,"小马哥");
f1.print();
f1.word();
f1.drve();
}
}
伸手就来。书那是一点不读
public class TestFather{
public static void main(String[] args){
Father father = new Father(20,"haha");
System.out.println("age:" + father.getAge());
System.out.println("name:" + father.name);
father.work();
father.drive();
}
}
class Father{
private int age;
public String name;
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public Father(int age, String name){
this.age = age;
this.name = name;
}
public void work(){
System.out.println("work() 被调用了...");
}
public void drive(){
System.out.println("drive() 被调用了...");
}
}