JAVA面向对象36道练习题代码总结

本文通过多个实例展示了Java中对象和类的定义及使用方法,包括属性、方法、构造器等核心概念,并介绍了继承、多态等面向对象编程特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 创建一个动物类 姓名 年龄 性别 会介绍自己
// 思考 如果在类中声明成员变量 没有给初值 那么有初值吗?
// 有 因为对象创建在堆内存(堆内存有默认的初始值)
// 睡觉方法 吃饭方法
public class Demo01 {
    public static void main(String[] args) {
        Animal animal = new Animal();
        animal.name = "猪";
        animal.age = 3;
        animal.kind = "公";
        animal.sayHi();
        animal.eat();
        animal.sleep();
    }
}
class Animal{
    String name;
    String kind;
    int age;

    public void eat() {
        System.out.println("动物会吃饭");
    }
    public void sleep() {
        System.out.println("动物喜欢睡觉");
    }

    public void sayHi() {
        System.out.println("姓名:" + name + "年龄:" + age + "种类" + kind);
    }
}

/*需求
 * 创建一个汽车类
 * 颜色
 * 牌子
 * 轮胎个数
 * 会动
 * 会撞人
 * 介绍自己的方法
 */
public class Demo02 {
    public static void main(String[] args) {
        Car car = new Car();
        car.type = "法拉利";
        car.color = "红色";
        car.num = 4;

        car.run();
        car.hitMan();
        car.sayHi();
    }
}
class Car{
    String type;
    String color;
    int num;

    public Car(){

    }

    public void run() {
        System.out.println("车子 可以 启动");
    }
    public void hitMan() {
        System.out.println("车子 可以 撞坏人");
    }
    public void sayHi() {
        System.out.println("类型:" + type);
        System.out.println("颜色:" + color);
        System.out.println("轮胎个数:" + num);
    }

}

 * 创建一个人 的  类
 * 
 * 属性  姓名  年龄
public class Demo03 {

    public static Person fun(String name) {
        Person person = new Person();
        person.name = name;
        person.sayHi();
        return person;
    }

    public static Person fun(String name, Person person) {
        Person person2 = person;
        person2.name = name;
        return person2;
    }


    public static void main(String[] args) {
        Person person = Demo03.fun("刘德华");
        Person person2 = fun("周润发", new Person());
        person2.sayHi();
    }
}
class Person{
    String name;
    int age = 20;

    public Person() {

    }
    public Person(String name) {
        this.name = name;
    }
    public void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
    }
}

创建一个学生类
私有化 姓名
私有化 年龄
package com.lanou3g;

public class Demo04 {
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("胡歌");
        student.setAge(30);

        System.out.println("姓名:" + student.getName());
        System.out.println("年龄:" + student.getAge());

        student.sayHi();
    }
}
class Student{
    private String name;
    private int age;

    public Student(){

    }
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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 void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
    }
}
创建10个类  
私有化4个属性
公共化3个方法
构造方法
set/get方法
自我介绍方法
package com.lanou3g;

public class Demo05 {
    public static void main(String[] args) {
        Animal1 animal = new Animal1();
        animal.setName("动物");
        animal.setAge(5);
        animal.setColor("黑");
        animal.setKind("公");

        System.out.println(animal.getName());
        System.out.println(animal.getColor());
        System.out.println(animal.getKind());
        System.out.println(animal.getAge());
        animal.sayHi();
    }
}
class Animal1{
    private String name;
    private String kind;
    private int age;
    private String color;

    public Animal1() {

    }
    public Animal1(String name, String kind, int age, String color) {
        this.name = name;
        this.age = age;
        this.kind = kind;
        this.color = color;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getKind() {
        return kind;
    }
    public void setKind(String kind) {
        this.kind = kind;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }

    public void eat(){
        System.out.println("动物要吃饭");
    }
    public void drink(){
        System.out.println("动物要喝水");
    }
    public void sleep(){
        System.out.println("动物要睡觉");
    }
    public void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
        System.out.println("种类:" + kind);
        System.out.println("颜色:" + color);
    }

}
package com.lanou3g;

public class Demo06 {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.setName("汤姆");
        cat.setAge(8);
        cat.setColor("灰白猫");
        cat.setKind("明星猫");

        System.out.println(cat.getName());
        System.out.println(cat.getColor());
        System.out.println(cat.getKind());
        System.out.println(cat.getAge());

        cat.sayHi();
    }
}
class Cat{
    private String name;
    private String color;
    private int age;
    private String kind;

    public Cat() {

    }
    public Cat(String name, String color, int age, String kind) {
        this.name = name;
        this.color = color;
        this.age = age;
        this.kind = kind;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getKind() {
        return kind;
    }
    public void setKind(String kind) {
        this.kind = kind;
    }

    public void catchMouse(){
        System.out.println("猫会抓老鼠");
    }
    public void shangTree(){
        System.out.println("猫会爬树");
    }
    public void speak(){
        System.out.println("猫会喵喵喵");
    }
    public void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
        System.out.println("颜色:" + color);
        System.out.println("种类:" + kind);
    }
}
public class Demo07 {
    public static void main(String[] args) {
        Leave leave = new Leave();
        leave.setColor("绿色");
        leave.setWide(25);
        leave.setLongSide(30);
        leave.setAge(3);
        System.out.println(leave.getColor());
        System.out.println(leave.getAge());
        System.out.println(leave.getWide());
        System.out.println(leave.getLongSide());
        leave.sayHi();
    }
}
class Leave{
    String color;
    int age;
    int wide;
    int longSide;

    public Leave() {

    }
    public Leave(String color, int age, int wide, int longSide) {
        this.color = color;
        this.age = age;
        this.wide = wide;
        this.longSide = longSide;
    }

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getWide() {
        return wide;
    }
    public void setWide(int wide) {
        this.wide = wide;
    }
    public int getLongSide() {
        return longSide;
    }
    public void setLongSide(int longSide) {
        this.longSide = longSide;
    }

    public void guangHe() {
        System.out.println("树叶会光合作用");
    } 
    public void huXi() {
        System.out.println("树叶会呼吸作用");
    }
    public void chengZhang() {
        System.out.println("树叶会成长");
    }
    public void sayHi() {
        System.out.println("树叶颜色:" + color);
        System.out.println("树叶宽度:" + wide);
        System.out.println("树叶年龄:" + age);
        System.out.println("树叶长度:" + longSide);
    }
}
package com.lanou3g;

public class Demo08 {
    public static void main(String[] args) {
        Monkey monkey = new Monkey();
        monkey.setName("齐天大圣");
        monkey.setType("石猴");
        monkey.setColor("五颜六色");
        monkey.setSex("男");

        System.out.println(monkey.getName());
        System.out.println(monkey.getType());
        System.out.println(monkey.getSex());
        System.out.println(monkey.getColor());
        monkey.sayHi();
    }
}
class Monkey{
    private String name;
    private String type;
    private String color;
    private String sex;

    public Monkey() {

    }
    public Monkey(String name, String type, String color,String sex) {
        this.name = name;
        this.type = type;
        this.color = color;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

    public void fun() {
        System.out.println("猴子 会 偷桃子");
    }
    public void fun2() {
        System.out.println("猴子 会 喝酒");
    }
    public void fun3() {
        System.out.println("猴子 会  刷棒子");
    }
    public void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("种类:" + type);
        System.out.println("颜色:" + color);
        System.out.println("性别:" + sex);
    }
}
package com.lanou3g;

public class Demo09 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.setName("哮天犬");
        dog.setColor("黑色");
        dog.setSex("公");
        dog.setType("神狗");

        System.out.println(dog.getName());
        System.out.println(dog.getColor());
        System.out.println(dog.getSex());
        System.out.println(dog.getType());
        dog.sayHi();
        dog.fun();
        dog.fun1();
        dog.fun2();
    }
}
class Dog{
    String name;
    String color;
    String sex;
    String type;

    public Dog() {

    }
    public Dog(String name, String color, String sex, String type) {
        this.name = name;
        this.color = color;
        this.sex = sex;
        this.type = type;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    } 

    public void fun() {
        System.out.println(name + " 会  咬 人");
    }
    public void fun1() {
        System.out.println(name + "跑 的 很快");
    }
    public void fun2() {
        System.out.println(name + "是人类的朋友");
    }
    public void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("颜色:" + color);
        System.out.println("类别:" + type);
        System.out.println("性别:" + sex );
    }
}
package com.lanou3g;

public class Demo10 {
    public static void main(String[] args) {
        Tank tank = new Tank();
        tank.setName("猛犸坦克");
        tank.setWeight(20);
        tank.setHigh(1);
        tank.setSpeed(22);

        System.out.println(tank.getName());
        System.out.println(tank.getHigh());
        System.out.println(tank.getWeight());
        System.out.println(tank.getSpeed());
        tank.fun1();
        tank.fun2();
        tank.fun3();
        tank.sayHi();
    }
}
class Tank{
    private String name;
    private int weight;
    private int high;
    private int speed;

    public Tank() {

    }
    public Tank(String name, int weight, int high, int speed){
        this.name = name;
        this.weight = weight;
        this.high = high;
        this.speed = speed;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    public int getHigh() {
        return high;
    }
    public void setHigh(int high) {
        this.high = high;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void fun1() {
        System.out.println(name + "会开炮");
    }
    public void fun2() {
        System.out.println(name + "会转弯");
    }
    public void fun3() {
        System.out.println(name + "可以载人");
    }
    public void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("重量:" + weight + "吨");
        System.out.println("高度:" + high + "米");
        System.out.println("速度:" + speed + "公里/时");
    }
}
import java.util.jar.Attributes.Name;

public class Demo11 {
    public static void main(String[] args) {
        Tiger tiger = new Tiger();
        tiger.setName("花花");
        tiger.setType("华南虎");
        tiger.setSex("母");
        tiger.setColor("黄色");

        System.out.println("姓名:" + tiger.getName());
        System.out.println("种类:" + tiger.getType());
        System.out.println("性别:" + tiger.getSex());
        System.out.println("肤色:" + tiger.getColor());

        tiger.fun();
        tiger.fun1();
        tiger.fun2();
        tiger.sayHi();

    }
}
class Tiger{
    private String name;
    private String type;
    private String color;
    private String sex;

    public Tiger() {

    }
    public Tiger(String name, String type, String color, String sex) {
        this.name = name;
        this.type = type;
        this.sex = sex;
        this.color = color;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

    public void fun() {
        System.out.println(name + "会吃人");
    }
    public void fun1() {
        System.out.println(name + "是森林之王");
    }
    public void fun2() {
        System.out.println(name + "会生小老虎");
    }

    public void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("性别:" + type);
        System.out.println("颜色:" + color);
        System.out.println("类型:" + type);
    }

}
package com.lanou3g;

public class Demo20 {
    public static void main(String[] args) {
        Tree tree = new Tree();
        tree.setName("杨树");
        tree.setAge(100);
        tree.setType("针叶林");
        tree.setHigh(12.5);
        tree.fun();
        tree.fun1();
        tree.fun2();

        tree.sayHi();
    }
}
class Tree{
    private String name;
    private int age;
    private String type;
    private double high;

    public Tree() {

    }
    public Tree(String name, int age, String type, double high) {
        this.name = name;
        this.age = age;
        this.type = type;
        this.high = high;
    }

    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 getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public double getHigh() {
        return high;
    }
    public void setHigh(double high) {
        this.high = high;
    }

    public void fun() {
        System.out.println("树会光合作用");
    }
    public void fun1() {
        System.out.println("树会有氧呼吸");
    }
    public void fun2() {
        System.out.println("树 无私奉献");
    }

    public void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("树龄:" + age);
        System.out.println("类型:" + type);
        System.out.println("树高:" + high);
    }
}

package com.lanou3g.bean1;

public class Caat {
    public void sayHi() {
        System.out.println("我是bean1 包下的 猫");
    }
}
package com.lanou3g;


public class Demo12 {
    public static void main(String[] args) {
        com.lanou3g.bean1.Caat cat = new com.lanou3g.bean1.Caat();
    }
}

package com.lanou3g;

public class Demo13 {
    public static void main(String[] args) {
        Car1 car = new Car1();
        car.name = "法拉利";
        car.money = 100000; 
        car.sayHi();
        car.fun();

        Car1 car2 = new Car1("奔驰", 50000);
        car2.sayHi();
    }
}
class Car1{

    String name;
    int money;

    public Car1() {
        System.out.println("我是有参构造方法");
    }
    public Car1(String name, int money) {
        this.name = name;
        this.money = money;
        System.out.println("我是有参构造方法");
    }

    public void sayHi(){
        System.out.println(name + money + "元");
    }
    public void fun() {
        System.out.println("你一定买的起");
    }
}

矩形 求周长 面积
package com.lanou3g;

public class Demo14 {
    public static void main(String[] args) {
        Trangle trangle = new Trangle(3,5);
        System.out.println(trangle.getLength());
        System.out.println(trangle.getArea());
    }
}
class Trangle{
     private int side;
     private int wide;
     private int length;
     private int area;

     public Trangle() {

     }
     public Trangle(int side, int wide) {
         this.side = side;
         this.wide = wide;  
     }

     public int getSide() {
         return side;
     }
     public void setSide(int side) {
         this.side = side;
     }
     public int getLength() {
         return (wide + side)*2;
     }
     public int getArea() {
         return side*wide;
     }

}

测试静态变量
package com.lanou3g;

public class Demo15 {
    public static void main(String[] args) {
        AVGirl avGirl = new AVGirl("波多老师");
        AVGirl avGirl2 = new AVGirl("苍老师");
        avGirl.country = "日本";
        avGirl2.country = "世界";
        avGirl.sayHi();
        avGirl2.sayHi();


    }
}
class AVGirl{
    String name;
    static String country;
    public AVGirl() {

    }
    public AVGirl(String name) {
        this.name = name;
    }
    public void sayHi() {
        System.out.println("姓名:" + name + "  国籍:" + country);
    }
}

/非静态方法  可以访问 静态变量 和 非静态变量
//静态方法 可以访问啥?(加上静态 相当于 随着类改变 跟对象没关系)
package com.lanou3g;

public class Demo16 {

}
class Man{
    static int num1 = 12;
    int num2 = 15;

    public void fun() {
        System.out.println(num1);    //正确 非静态方法可以访问静态变量
        System.out.println(num2);    //正确 非静态方法可以访问非静态变量
    }

    public static void fun1() {
        System.out.println(num1);  //正确 静态方法可以访问静态变量
     // System.out.println(num2);  //错误 静态方法不可以访问非静态变量
    }
}

十一

/*
 * 打印数组
 * 反转数组
 * 找最大值
 * 冒泡排序
 */
 ```
 ```
public class Demo17 {
    public static void main(String[] args) {

    }

    public void fun(int[] arr) {
        for(int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

    }
    public int fun1(int[] arr) {
        int key = 0;
        for(int i = 0; i < arr.length; i++) {
            if(key < arr[i]) {
                key = arr[i];
            }
        }
        return key;
    }
    public void fun3(int[] arr) {
        for(int i = 0; i < arr.length/2; i++) {
            int temp = arr[i];
            arr[i] = arr[arr.length - 1 -i];
            arr[arr.length - 1 - i] = arr[i];
        }
    }
    public void fun4(int[] arr) {
        for(int i = 1; i < arr.length; i++) {
            for(int j = 0; i< arr.length -i; j++) {
                if(arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j +1] = temp;
                }
            }
        }
    }
}

十二

package com.lanou3g;

public class Demo20 {
    public static void main(String[] args) {
        Employ employ = new Employ("刘德华",2456);
        employ.setMoney(6000);
        employ.sayHi();
    }
}
class Employ{
    private String name;
    private int id;
    private int money;

    public Employ() {

    }
    public Employ(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }

    public void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("Id:" + id);
        System.out.println("工资:" + money);
    }
}

十三

package com.lanou3g;

import java.util.jar.Attributes.Name;

public class Demo19 {
    public static void main(String[] args) {
        Teacher teacher = new Teacher("王龙",15);
        Student1 student = new Student1("刘荣胜", 22, 20144);
        System.out.println(teacher);
        System.out.println(student);

    }
}
class Mann{
    private String name;
    private int age;

    public Mann(){

    }
    public Mann(String name, int age) {
        this.age = age;
        this.name = name;
    }

    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 void eat() {
        System.out.println("人类需要吃饭");
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "姓名:" + name + "年龄:" + age;
    }
}
class Teacher extends Mann{
    public Teacher() {

    }
    public Teacher(String name, int age) {
        super(name,age);
    }

    public void teach() {
        System.out.println(this.getName() + "会讲课");
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }   
}
class Student1 extends Mann{
    private int number;

    public Student1() {

    }
    public Student1(String name, int age, int number) {
        super(name,age);
        this.number = number;
    }

    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }

    public void study() {
        System.out.println(this.getName() + "在上课");
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString() + "学号:" + number;
    }
}

十四

package com.lanou3g;

public class Demo22 {
    public static void main(String[] args) {
        Fish fish = new Fish();
        fish.setName("金鱼");
        fish.setAge(2);
        fish.setWeight(2.5);
        fish.setLength(0.12);
        fish.eat();
        fish.breath();
        fish.swim();
        fish.sayHi();
    }
}
class Fish{
    private String name;
    private int age;
    private double weight;
    private double length;

    public Fish() {

    }
    public Fish(String name, int age, double weight, double length) {
        this.name = name;
        this.age = age;
        this.weight =weight;
        this.length = length;
    }

    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 getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public double getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }

    public void swim() {
        System.out.println("鱼会游泳");
    }
    public void breath() {
        System.out.println("鱼用腮呼吸");
    }
    public void eat() {
        System.out.println("鱼会吃饵料");
    }
    public void sayHi() {
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
        System.out.println("重量:" + weight);
        System.out.println("长度:" + length);
    }
}

十五

代码块
public class Demo01 {
    {
         int a = 10;
         System.out.println(a);
         System.out.println("我是Demo24构造代码块");
    }
    public static void main(String[] args) {
        Person2 person = new Person2();
        person.name = "张三";
        person.age = 15;
        person.sayHi();
        System.out.println("我是main函数中得普通方法");
        {
            System.out.println("我是局部代码块");
        }
        Person2 person1 = new Person2("李四",22);
        person1.sayHi();
    }
}
class Person2{
    String name;
    int age;
    //第一个代码块
    static {
          System.out.println("我是person类的静态代码块");
    }
    //第二个代码块
    {
        //每一个对象 都会调用同一个方法 这时可以使用
           sleep();
           System.out.println("我是构造代码块");
    }
    public Person2(){
           System.out.println("我是无参数的构造方法");
    }
    public Person2(String name, int age){
          this.name = name;
          this.age = age;
          System.out.println("我是有参数的构造方法");
    }

    public String getName(){
            return name;
    }
    public void setName(){
            this.name = name;
    }
    public int getAge(){
            return age;
    }
    public void setAge(int age){
             this.age = age;
    }

    public void sayHi(){
           System.out.println("姓名:" + name + "年龄:" + age);
    }

     public void sleep(){
          System.out.println("睡觉");
     }
}

结果:
我是person类的静态代码块
睡觉
我是构造代码块
我是无参数的构造方法
姓名:张三年龄:15
我是main函数中得普通方法
我是局部代码块
睡觉
我是构造代码块
我是有参数的构造方法
姓名:李四年龄:22

十六

package com.lanou3g.bean1;

public class Demo02 {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.name = "花花";
        cat.color = "白色";
        cat.age = 3;
        cat.sayHi();
        cat.catchMouse();

        Dog dog = new Dog();
        dog.name = "旺福";
        dog.color = "灰色";
        dog.age = 2;
        dog.sayHi();
        dog.lookDoor();
    }
}
class Animal{
    String name;
    int age;
    String color;

    public void sayHi() {
        System.out.println("姓名:" + name + "年龄:" + age + "颜色:" + color);
    }
}
class Cat extends Animal{
    public void catchMouse() {
        System.out.println("猫会 抓 老鼠");
    }
}
class Dog extends Animal{
    public void lookDoor() {
        System.out.println("狗会 看门");
    }
}

十七

package com.lanou3g.bean1;

public class Demo03 {
    public static void main(String[] args) {
        Son son = new Son();
        son.name = "张三";
        son.sayHi();
        Son son2 = new Son("李四");
        son2.sayHi();
    }
}
class Father{
    String name;
    public Father(){
        System.out.println("我是父类的无参构造方法");
    }
    public Father(String name) {
        this.name = name;
        System.out.println("我是父类的有参构造方法");
    }
    public void sayHi() {
        System.out.println("姓名:" + name);
    }
}

class Son  extends Father{
    public Son() {
        System.out.println("我是子类的无参构造方法");
    }
    public Son(String name) {
        super(name);
        System.out.println("我是子类的有参构造方法");
    }
}

十八

package com.lanou3g.bean1;

public class Demo04 {
    public static void main(String[] args) {
        TestB testB = new TestB();
        testB.fun();
        testB.fun1();
        testB.fun2();
    }
}
class TestA{
    int num1 = 30;
    int num2 = 20;

    public TestA(){
        System.out.println("我是TestA类的构造方法");
    }
}

class TestB extends TestA{
    int num1 = 10;
    public void fun() {
        System.out.println(num1);
    }
    public void fun1() {
        System.out.println(num2);
    }
    public void fun2() {
        System.out.println(super.num1);
        System.out.println(super.num2);
    }
}

十九

package com.lanou3g.bean1;

public class Demo05 {
    public static void main(String[] args) {
        Note note = new Note();
        note.sayHi();
    }
}
class Phone{
    String name;

    public Phone() {
        System.out.println("我是无参数的构造方法");
    }
    public Phone(String name) {
        this.name = name;
        System.out.println("我是有参数的构造方法");
    }
    public void sayHi() {
        System.out.println(name);
    }
}

class Note extends Phone{
    public Note(){
        super("Note2");
    }
    public Note(String name) {
        super(name);
    }
}

二十

package com.lanou3g.bean1;

public class Demo06 {
    public static void main(String[] args) {
        IOS8 ios8 = new IOS8();
        ios8.sayEngchin();
        System.out.println(ios8);
        System.out.println(ios8.toString());

        IOS7 ios7 = new IOS7();
        ios7.sayEnglish();
        System.out.println(ios7);
    }
}
class IOS7{
    public void call() {
        System.out.println("IOS7可以打电话");
    }
    public void sayEnglish() {
        System.out.println("IOS7会说英文");
    }
}
class IOS8 extends IOS7{
    public void sayEngchin() {
        super.sayEnglish();
        System.out.println("会说中文");
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "说中文";
    }
}

二十一

package com.lanou3g.bean1;

import javax.xml.transform.Templates;

public class Demo07 {
    public static void main(String[] args) {
        Student student = new Student("刘德华",32,20144);
        student.eat();
        student.study();
        System.out.println(student);

        Teacher teacher = new Teacher("刘亦菲",22);
        teacher.eat();
        teacher.teach();
        System.out.println(teacher);
    }
}
class Person{
    private String name;
    private int age;

    public Person() {

    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    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 void eat() {
        System.out.println("人需要吃饭");
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "姓名:" + name + "年龄:" + age;
    }
}
class Student extends Person {
    private int number;

    public Student() {

    }
    public Student(String name, int age, int number) {
        super(name,age);
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }

    public void study() {
        System.out.println("学生需要上学");
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString() + "学号:" + number;
    }
}
class Teacher extends Person{
    public Teacher() {

    }
    public Teacher(String name, int age) {
        super(name, age);
    }

    public void teach() {
        System.out.println("老师需要讲课");
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }
}

二十二

* 猫类
 *名字 毛色 腿数 猫眼的类型 猫叫喵喵喵
 *狗类
 *名字 毛色 腿数  狗叫汪汪汪
 *要求:使用继承 无参 有参构造方法 set/get方法 介绍自己的方法
public class Demo08 {
    public static void main(String[] args) {
        Cat3 cat = new Cat3();
        cat.setName("花花");
        cat.setColor("白色");
        cat.setNumber(4);
        cat.setEyeType("棕色");
        System.out.println(cat);
        Dog3 dog = new Dog3("旺仔","黑色",4);
        System.out.println(dog);
    }
}
class Animal2{
    private String name;
    private String color;
    private int number;

    public Animal2(){

    }
    public Animal2(String name, String color, int number){
        this.name = name;
        this.color = color;
        this.number = number;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "姓名:" + name + "颜色:" + color + "腿的数量:" + number;
    }
}

class Cat3 extends Animal2{
    private String eyeType;
    public Cat3() {

    }
    public Cat3(String name, String color, int number) {
        super(name, color, number);
        this.eyeType = eyeType;
    }
    public String getEyeType() {
        return eyeType;
    }
    public void setEyeType(String eyeType) {
        this.eyeType = eyeType;
    }
    public void speak() {
        System.out.println("猫会喵喵叫");
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString() + "眼睛类型" + eyeType;
    }
}
class Dog3 extends Animal2{
    public Dog3() {

    }
    public Dog3(String name, String color, int number) {
        super(name, color, number);
    }

    public void speak() {
        System.out.println("狗会旺旺叫");
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }
}

二十三

package com.lanou3g.bean2;

public class Demo01 {
    public static void main(String[] args) {
        ClassB classB = new ClassB(23);
        classB.fun();
        classB.fun1();
        ClassA classA = new ClassA();
        System.out.println(classA.num);
        System.out.println(classB.num);
    }
}
class ClassA{
    int num = 10;
    public ClassA() {

    }
    public ClassA(int num) {
        this.num = num;
    }
    public final void fun() {
        System.out.println("我是被final修饰的方法" + num);
    }
    public void fun1() {
        System.out.println("我是普通的方法");
    }
}

class ClassB extends ClassA{
    public ClassB() {
        // TODO Auto-generated constructor stub
    }
    public ClassB(int num) {
        this.num = num;
    }
    @Override
    public void fun1() {
        // TODO Auto-generated method stub
        super.fun1();
    }
}

二十四

package com.lanou3g.bean2;

public class Demo02 {
    public static void main(String[] args) {
        Anim anim = new Cattt("花花","中华田园猫");
        Anim anim2 = new Doggg("汤姆","美国狗");
        anim.speak();
        System.out.println(anim);
        anim2.speak();
        System.out.println(anim2);
    }
}
class Anim{
    String name;
    String kind;

    public Anim(){

    }
    public Anim(String name, String kind) {
        this.name = name;
        this.kind = kind;
    }

    public void speak() {
        System.out.println("动物会发出叫声");
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "姓名: " + name + " 种类: " + kind;
    }
}

class Cattt extends Anim{
    public Cattt() {

    }
    public Cattt(String name, String kind) {
        this.name = name;
        this.kind = kind;
    }
    @Override
    public void speak() {
        // TODO Auto-generated method stub
        System.out.println("猫会喵喵叫");;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }
}
class Doggg extends Anim{
    public Doggg(){

    }
    public Doggg(String name, String kind) {
        this.name = name;
        this.kind = kind;
    }
    @Override
    public void speak() {
        // TODO Auto-generated method stub
        System.out.println("狗会汪汪汪叫");;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }
}

二十五

package com.lanou3g.bean2;

public class Demo03 {
    public static void main(String[] args) {
        Father father = new Son();
        System.out.println(father.num);
        father.sayHi();
    }
}
class Father{
    int num = 1;
    public void sayHi() {
        System.out.println("我是父类的print方法");
    }
}
class Son extends Father{
    int num1 = 2;
    public void sayHi() {
        System.out.println("我是子类的print方法");
    }
}

二十六

package com.lanou3g.bean2;

public class Demo04 {
    public static void main(String[] args) {
        Per per = new Pz();
        per.speak();

        Pz pz = (Pz)per;
        pz.hit();
    }
}
class Per{
    public void speak() {
        System.out.println("");
    }
}
class Pz extends Per{
    @Override
    public void speak() {
        // TODO Auto-generated method stub
        System.out.println("洗脑");;
    }

    public void hit() {
        System.out.println("打人");
    }
}

二十七

package com.lanou3g.bean2;

public class Demo05 {
    public static void Wq(WQ wq) {
        wq.play();

        if(wq instanceof Dao) {
            Dao dao = (Dao)wq;
            dao.practise();
        }
        if(wq instanceof Gun) {
            Gun gun = (Gun)wq;
             gun.practise();
        }
    }
    public static void main(String[] args) {
        Wq(new Dao());
        Wq(new Gun());
    }
}
class WQ{
    public void play() {
        System.out.println("挥舞武器");
    }
}
class Dao extends WQ{
    @Override
    public void play() {
        // TODO Auto-generated method stub
        System.out.println("挥舞刀 砍人");;
    }

    public void practise() {
        System.out.println("练刀");
    }
}
class Gun extends WQ{
    @Override
    public void play() {
        // TODO Auto-generated method stub
        System.out.println("挥舞棒 敲人");;
    }

    public void practise() {
        System.out.println("练棍");
    }
}

二十八

/*
 * 铁桶僵尸(zombie) 血量 方法:被打一次掉2血 直到被打死
 * 帽子僵尸 血量 方法:被打一次掉5血 直到被打死
 * 封装一个 打僵尸的方法
 *
public class Demo06 {
    public static void main(String[] args) {
        zombie(new TTZombie(30));
        zombie(new MZZombie(40));
    }
    public static void zombie(Zombie zombie) {
        zombie.play();
    }
}
class Zombie{
    private int blood;

    public Zombie() {

    }
    public Zombie(int blood) {
        this.blood = blood;
    }
    public int getBlood() {
        return blood;
    }
    public void setBlood(int blood) {
        this.blood = blood;
    }
    public void  play() {
        System.out.println("僵尸被打了");
    }
}

class TTZombie extends Zombie{
    public TTZombie(){

    }
    public TTZombie(int blood) {
        super(blood);
    }
    @Override
    public void play() {
        // TODO Auto-generated method stub
        while (true) {
            if(this.getBlood() <= 0) {
                System.out.println("铁通僵尸被打死了");
                break;
            }
            this.setBlood(this.getBlood() - 2);
            System.out.println("铁桶僵尸剩余血量:" + this.getBlood());
        }
    }
}

class MZZombie extends Zombie{
    public MZZombie() {

    }
    public MZZombie(int blood) {
        super(blood);
    }
    @Override
    public void play() {
        // TODO Auto-generated method stub
        if(this.getBlood() <= 0) {
            return ;
        }
        this.setBlood(this.getBlood() - 5);
        System.out.println("帽子僵尸剩余血量:" + this.getBlood());
        play();
    }
}

二十九

 */
public class Demo07 {
    public static void main(String[] args) {
        KeyBoard keyBoard = new KeyBoard();
        keyBoard.run();
        keyBoard.insertCard(new NetCard());
        keyBoard.insertCard(new MusicCard());
    }
}
class KeyBoard{
    public void run() {
        System.out.println("电脑已经启动");
    }
    public void insertCard(Card card) {
        card.start();
        card.stop();
    }

}
class Card{
    public void start() {
        System.out.println("卡已插入");
    }
    public void stop() {
        System.out.println("卡已拔出");
    }
}
class MusicCard extends Card{
    @Override
    public void start() {
        // TODO Auto-generated method stub
        System.out.println("音乐已播放");;
    }
    @Override
    public void stop() {
        // TODO Auto-generated method stub
        System.out.println("音乐已停止");;
    }
}
class NetCard extends Card{
    @Override
    public void start() {
        // TODO Auto-generated method stub
        System.out.println("网已经连上");;
    }
    @Override
    public void stop() {
        // TODO Auto-generated method stub
        System.out.println("网已经断开");;
    }
}

三十

package com.lanou3g.bean2;

public class Demo08 {
    public static void main(String[] args) {
        Contral control = new Contral();
        control.run();
        control.siri(new Contra());
        control.siri(new Dragon());
        control.siri(new Island());
    }
}

class Contral{
    public void run() {
        System.out.println("游戏机已启动");
    }
    public void siri(MyCard myCard) {
        myCard.start();
        myCard.stop();
    }
}
class MyCard{
    public void start() {
        System.out.println("游戏已开始");
    }
    public void stop() {
        System.out.println("游戏已结束");
    }
}
class Contra extends MyCard {
    @Override
    public void start() {
        // TODO Auto-generated method stub
        System.out.println("魂斗罗游戏已启动");;
    }
    @Override
    public void stop() {
        // TODO Auto-generated method stub
        System.out.println("魂斗罗游戏已关闭");;
    }
}
class Dragon extends MyCard{
    @Override
    public void start() {
        // TODO Auto-generated method stub
        System.out.println("双截龙游戏已启动");;
    }
    @Override
    public void stop() {
        // TODO Auto-generated method stub
        System.out.println("双截龙游戏已关闭");;
    }
}
class Island extends MyCard{
    @Override
    public void start() {
        // TODO Auto-generated method stub
        System.out.println("冒险岛游戏已启动");;
    }
    @Override
    public void stop() {
        // TODO Auto-generated method stub
        System.out.println("冒险岛游戏已关闭");
    }
}

三十一

package com.lanou3g.bean2;

import java.awt.event.WindowEvent;

public class Demo09 {
    public static void main(String[] args) {
        Calculate calculate = new Calculate();
        calculate.calculate(new Circle(3));
        calculate.calculate(new Triangle(3,6));
        calculate.calculate(new Rectangle(6,9));
    }
}
class Calculate{
    public void run() {
        System.out.println("计算功能已开启");
    }

    public void calculate(Graphic graphic) {
        graphic.getLength();
        graphic.getArea();
    }
}
class Graphic{
    private double wide;
    private double high;
    private double r;

    public Graphic() {

    }
    public Graphic(double r) {
        this.r = r;
    }
    public Graphic(double wide, double high) {
        this.wide = wide;
        this.high = high;
    }

    public double getWide() {
        return wide;
    }
    public void setWide(double wide) {
        this.wide = wide;
    }
    public double getHigh() {
        return high;
    }
    public void setHigh(double high) {
        this.high = high;
    }
    public double getR() {
        return r;
    }
    public void setR(double r) {
        this.r = r;
    }

    public void getLength() {
        System.out.println("求该图形的周长");
    }
    public void getArea() {
        System.out.println("求该图形的面积");
    }
}

class Circle extends Graphic{
    public Circle() {

    }
    public Circle(double r) {
        super(r);
    }

    @Override
    public void getLength() {
        // TODO Auto-generated method stub
        System.out.println("圆的周长为:" + 2*3.14*this.getR());;
    }
    @Override
    public void getArea() {
        // TODO Auto-generated method stub
        System.out.println("圆的面积为:" + 3.14*this.getR()*this.getR());;
    }
}
class Rectangle extends Graphic{
    public Rectangle() {

    }
    public Rectangle(double wide, double high) {
        super(wide,high);
    }
    @Override
    public void getLength() {
        // TODO Auto-generated method stub
        System.out.println((this.getHigh() + this.getWide())*2);
    }
    @Override
    public void getArea() {
        // TODO Auto-generated method stub
        System.out.println(this.getHigh()*this.getWide());;
    }
}
class Triangle extends Graphic{
    public Triangle() {

    }
    public Triangle(double wide, double high) {
        super(wide,high);
    }

    @Override
    public void getLength() {
        // TODO Auto-generated method stub
        System.out.println(this.getWide()*3);
    }
    @Override
    public void getArea() {
        // TODO Auto-generated method stub
        System.out.println(0.5*this.getWide()*this.getHigh());;
    }

三十二

package com.lanou3g.bean3;

public class Demo01 {
    public static void main(String[] args) {
        Person person = new Student();
        person.eat();
        person.sleep();
    }
}
abstract class Person{
    public abstract void eat();
    public abstract void sleep();
}
class Student extends Person{

    @Override
    public void eat() {
        // TODO Auto-generated method stub
        System.out.println("学生需要吃饭");
    }

    @Override
    public void sleep() {
        // TODO Auto-generated method stub
        System.out.println("学生需要休息");
    }

}

三十三

public class Demo02 {
    public static void main(String[] args) {
        BXJP bxjp = new LPZ();
        BXJP bxjp2 = new YBQ();
        bxjp.ZG();
        bxjp2.ZG();
    }
}
abstract class BXJP{
    public abstract void ZG();
}
class LPZ extends BXJP{

    @Override
    public void ZG() {
        // TODO Auto-generated method stub
        System.out.println("狼牙棒自宫");
    }

}
class YBQ extends BXJP{

    @Override
    public void ZG() {
        // TODO Auto-generated method stub
        System.out.println("流星锤自宫");
    }

}

三十四

package com.lanou3g.bean3;

public class Demo03 {
    public static void main(String[] args) {
        fun(new DotMatriixPrinter());
        fun(new InkperPrinter());
        fun(new LaserPrinter());;
    }
    public static void fun(Printer printer) {
        printer.print();
    }
}
abstract class Printer{
    public abstract void print();
}
class DotMatriixPrinter extends Printer{
    @Override
    public void print() {
        // TODO Auto-generated method stub
        System.out.println("我是针式打印机");
    }   
}
class InkperPrinter extends Printer{

    @Override
    public void print() {
        // TODO Auto-generated method stub
        System.out.println("我是喷墨式打印机");
    }

}
class LaserPrinter extends Printer{

    @Override
    public void print() {
        // TODO Auto-generated method stub
        System.out.println("我是激光打印机");
    }

}

三十五

package com.lanou3g.bean3;

public class Demo04 {
    public static void main(String[] args) {
        rent(new Car("宝马"));
        rent(new Bus(23));
    }
    public static void rent(Vehicle wVehicle) {
        wVehicle.rent();
    }
}
abstract class Vehicle{
    public abstract void rent();
}
class Car extends Vehicle{
    private String name;
    public Car() {

    }
    public Car(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public void rent() {
        // TODO Auto-generated method stub
        System.out.println(name + "汽车 100 一天");
    }   
}
class Bus extends Vehicle{
    private int num;
    public Bus() {

    }
    public Bus(int number) {
        this.num = number;
    }
    @Override
    public void rent() {
        // TODO Auto-generated method stub
        if(num == 18) {
            System.out.println("18座的小汽车200一天");
        }else if(num == 23) {
            System.out.println("23座的小汽车250一天");
        }else {
            System.out.println("只有18座和23座的汽车");
        }
    }

}

三十六

package com.lanou3g.bean3;

import java.util.Scanner;

public class Personer {
    public static int fun() {
            Scanner scanner = new Scanner(System.in);
            while(true) {
                System.out.println("请输入 石头 剪刀 布:");
                String string = scanner.nextLine();
                switch (string) {
                case "石头":
                    System.out.println("人出:石头");
                    return 1;
                case "剪刀":
                    System.out.println("人出:剪刀");
                    return 2;
                case "布":
                    System.out.println("人出:布");
                    return 3;
                default:
                    System.out.println("输入不符合要求");
                    break;
                }
        }
    }
}

package com.lanou3g.bean3;

public class Computers {
    public static int fun() {
        int number = (int)(Math.random()*(3 - 1 + 1) + 1);
        switch (number) {
        case 1:
            System.out.println("电脑出:石头");
            break;
        case 2:
            System.out.println("电脑出:剪刀");
            break;
        case 3:
            System.out.println("电脑出:布");
            break;
        default:
            break;
        }
        return number;
    }
}


package com.lanou3g.bean3;

public class TestWin {
    public static void main(String[] args) {
        int number1 = Personer.fun();
        int number2 = Computers.fun();
        int rel = number1 - number2;

        if(rel == -1 || rel == 2) {
            System.out.println("人赢");
        }else if(rel == -2 || rel == 1) {
            System.out.println("电脑赢");
        }else {
            System.out.println("平局");
        }

    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值