类的定义 对象的创建 方法的调用

1、写一个Student类,定义姓名(name)、年龄(age)、性别(sex) 等属性,还有学习(study)方法,并提供有参和无参两个构造 函数。另写一个Test类定义主函数,创建三个Student类型 对象并给每个对象的属性进行赋值,调用study方法。

2、写一个Dog类,定义颜色(color)、年龄(age)等属性,还有睡 (sleep)方法,并提供有参和无参两个构造 函数。另写一个 Test类定义主函数,创建三个Dog类型对象并给每个对象的 属性进行赋值,调用sleep方法。

3、写一个Teacher类,定义姓名(name)、年龄(age)、工号(number) 等属性,还有吃饭(eat)方法,并提供有参和无参两个构造 数。另写一个Test类定义主函数,创建三个Teacher类型 象并给每个对象的属性进行赋值,调用eat方法。

4、写一个汽车(Car)类,定义颜色(color)、名字(name),等属性还 有跑(run)的方法,并提供有参和无参两个构造函数。另写一 Test类定义主函数,创建三个Car类型 象并给每个对 象的属性进行赋值,调用run方法。

5、写一个猫(Cat)类,定义颜色(color)、名字(name)、体重(weight) 等属性,还有玩耍(play)的方法,并提供有参和无参两个构造 函数。另写一个Test类定义主函数,创建三个Cat类型对 并给每个对象的属性进行赋值,调用play方法。

package day12_oo;


/*
 * 定义各种类
 * 构造函数:1.构造函数必须和类名一样(包括大小写) 2.构造函数不允许手工调用 ,在对象的构造过程中自动调用一次
 * 3.构造函数没有返回值类型(意思连void都没有) 
 * 如果一个类没有定义任何构造方法,系统会默认添加一个公开的无参构造方法
 */
public class Test {
    public static void main(String[] args){
        Student alex = new Student("Alex",23,true);
        Student jay = new Student("Jay",23,true);
        Student moses = new Student("Moses",23,true);
        alex.study("Alex");
        
        Dog pointpoint = new Dog("waite",2);
        Dog smallyellow = new Dog("yellow",1);
        Dog smallduck = new Dog("orange",2);
        smallyellow.sleep("smallyellow");
        
        Teacher zhang = new Teacher("zhang",25,"201610812188");
        Teacher wang = new Teacher("wang",22,"201610812105");
        Teacher wu = new Teacher("wu",18,"201610812166");
        wu.eat("wu");
        
        Car bmw = new Car("waite","bmw");
        Car byd = new Car("red","byd");
        Car wlrg = new Car("green","wlrg");
        wlrg.run("wlrg");
        
        Cat mimi = new Cat("waite","mimi",15.0);
        Cat miaomiao = new Cat("black","miaomiao",8.0);
        Cat xixi = new Cat("blue","xixi",10.0);
        mimi.play("mimi");
    }
    
}
//定义一个学生类还有学习方法
class Student{
    String name;
    int age;
    boolean sex;
public Student(){}//无参构造方法
public Student(String name,int age,boolean sex){}//有参构造方法
    void study(String name){
        System.out.println(name+"正在学习");
    }
}
//定义一个Dog类还有睡觉方法
class Dog{
    String color;
    int age;
    public Dog(){}//无参构造方法
    public Dog(String color,int age){}//有参构造方法
    void sleep(String color){
        System.out.println(color+"正在睡觉");
    }
}
//写一个Teacher类,定义姓名(name)、
//年龄(age)、工号(number)    等属性,还有吃饭(eat)方法。
class Teacher{
    String name;
    int age;
    String number;
    public Teacher(){}//无参构造方法
    public Teacher(String name,int age,String number){}//有参构造方法
    void eat(String name){
        System.out.println(name+"老师在吃饭");
    }
}

//写一个汽车(Car)类,定义颜色(color)、名字(name),等属性还
//4、有跑(run)的方法。
class Car{
    String color;
    String name;
    public Car(){}
    public Car(String color,String name){}
    void run(String name){
        System.out.println(name+"车在跑");
    }
}

//写一个猫(Cat)类,定义颜色(color)、名字(name)、体重(weight)
//5、    等属性,还有玩耍(play)的方法。
class Cat{
    String color;
    String name;
    Double weight;
    public Cat(){}//无参构造方法
    public Cat(String color,String name,double weigth){}//有参构造方法
     void play(String name){
         System.out.println(name+"小猫正在玩");
     }
}

猜你喜欢

转载自www.cnblogs.com/alexliuf/p/13173241.html