面向对象1练习

this测试代码

package wangKe.liaXi01;

public class TestThis {
    //成员变量
    int a,b,c;

    //定义一个空方法
    TestThis(){
        System.out.println("正要new一个Hello对象");
    }
    //定义一个带有两个int类型的方法
    TestThis(int a, int b){
        //Hello();      这样是无法调用构造方法的!
        this();         //调用无参的构造方法必须在第一行
        a = a ;         //这里指的是局部变量不是成员变量
        this.a = a ;    //这样就区分开局部变量和成员变量
        this.b = b ;
    }
    //定义一个三个int类型的方法
    TestThis(int a , int b ,int c ){
        this(a, b);     //调用无参的方法必须在第一行
        this.c = c ;
    }
    //定义一个空方法
    void sing(){}
    //定一个吃饭的方法
    void chiFan(){
        this.sing();            //sing();这两种都可以
        System.out.println("你妈妈喊你回家吃饭!");
    }
    //main方法
    public static void main(String[] args) {
        
        TestThis tt = new TestThis();
        
        tt.chiFan();
    }
}

Static示例代码

package wangKe.liaXi01;

public class TestStatic {
    //成员变量
    int a ;
    //静态变量
    static int width;
    //静态方法
    static void gg(){
        System.out.println("gg");
    }
    //普通方法
    void tt(){
        System.out.println("tt");
    }
    //main方法
    public static void main(String[] args){
        //创建对象
        TestStatic hs = new TestStatic();
        TestStatic.width = 2;
        //三种调用格式
        TestStatic.gg(); //gg();
        hs.gg();
        gg();//通过引用也可以访问static变量或static方法。不过,一般还是使用类名.static成员名来访问。
    }

}

Static关键字

/*
使用static声明的成员变量称为静态变量,
使用static声明的方法称为静态方法
静态变量不静态方法又称为类变量和类方
 */
package wangKe.liaXi01;
/*
使用static统计在类中一共产生多个对象
 */
public class StaticDemo {//声明类
    static int count;//声明静态属性

    public StaticDemo() {//无参构造方法
        count++;
        System.out.println("创建了" + count + "个对象");
    }

    public static void main(String[] args) {
        new StaticDemo();//创建匿名对象
        new StaticDemo();//创建匿名对象
        new StaticDemo();//创建匿名对象
    }
}

常见错误

public void showInfo(){
System.out.println("姓名:"+this.name+"\t年龄:"+this.age+"\t城
市:"+this.country);
}
public static void welcome(){
this.showInfo();//调用本类的非静态方法
System.out.println("欢迎大家来腾迅互联学习......");
public class StaticDemo {
    class Dog {
        private String name = "旺财"; // 昵称
        private int health = 100; // 健康值
        private int love = 0;       //亲密度
        public void play(int n) {
            static int localv=5;      //在方法里不可以定义static变量
            health = health - n;

            System.out.println(name+" "+localv+" "+health+" "+love);
        }
        public static void main(String[] args) {
            Dog d=new Dog();
            d.play(5);
        }
    }
}

上机练习1——设计Dog和Penguin类

需求说明:

– 运用面向对象思想抽象出Dog类和Penguin类,画出对应类图

– 根据类图编写Dog类和Penguin类

– 添加默认构造方法

package wangKe.zuoYe01;

public class Dog {
    //成员变量
    private String name ;       //昵称
    private int healthValue;    //健康值
    private int intimacy;       //亲密度
    private  String varieties;  //品种

    //构造一个空方法
    public Dog(){}

    //构造一个全参的方法
    public Dog(String name, int healthValue, int intimacy, String varieties){
        setName(name);
        setHealthValue(healthValue);
        setIntimacy(intimacy);
        setVarieties(varieties);
    }
    //关于昵称的get/set方法
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name =name;
    }

    //关于健康值的get/set 方法
    public int  getHealthValue(){
        return healthValue;
    }
    public  void setHealthValue(int healthValue){
        if (healthValue<=0 || healthValue>=101){
            this.healthValue =80;
            System.out.println("您的输入有误!已为您设置为默认值80。");
        }else {
            this.healthValue=healthValue;

        }
    }

     //关于亲密度的get/set方法
    public int getIntimacy(){
        return  intimacy;
    }
    public void setIntimacy(int intimacy) {
        if (intimacy <= 0) {
            this.intimacy = 60;
            System.out.println("您的输入有误!已为您设置为默认值60。");
        }else {
            this.intimacy=intimacy;
        }
    }

    //关于品种的get/set 方法
    public String getVarieties(){
        return varieties;
    }
    public void setVarieties(String varieties) {
        System.out.println("请选择品种:1.二哈  2.中华田园犬");
        if (varieties.equals("1")) {
            this.varieties = "二哈";
        }else{
            this.varieties ="中华田园犬";
        }
    }
    //定义一个关于行为的方法
    private  void show(){
        System.out.println("昵称:"+name +"健康值:"+healthValue +"亲密度:"+intimacy + "品种:"+varieties);
    }
}
package wangKe.zuoYe01;

public class Penguin {
    //成员变量
    private String name;       //昵称
    private int healthValue;    //健康值
    private int intimacy;       //亲密度
    private String gender;     //性别

    //构造一个空方法
    public Penguin() {
    }

    //构造一个全参的方法
    public Penguin(String name, int healthValue, int intimacy, String varieties) {
        setName(name);
        setHealthValue(healthValue);
        setIntimacy(intimacy);
        setVarieties(varieties);
    }

    //关于昵称的get/set方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //关于健康值的get/set 方法
    public int getHealthValue() {
        return healthValue;
    }

    public void setHealthValue(int healthValue) {
        if (healthValue <= 0 || healthValue >= 101) {
            this.healthValue = 80;
            System.out.println("您的输入有误!已为您设置为默认值80。");
        } else {
            this.healthValue = healthValue;

        }
    }

    //关于亲密度的get/set方法
    public int getIntimacy() {
        return intimacy;
    }

    public void setIntimacy(int intimacy) {
        if (intimacy <= 0) {
            this.intimacy = 60;
            System.out.println("您的输入有误!已为您设置为默认值60。");
        } else {
            this.intimacy = intimacy;
        }
    }

    //关于品种的get/set 方法
    public String getGender() {
        return gender;
    }

    public void setVarieties(String gender) {
        System.out.println("请选择品种:1.雄性  2.雌性");
        if (gender.equals("1")) {
            this.gender = "雄性";
        } else if (gender.equals("2")) {
            this.gender = "雌性";
        }else {
            System.out.println("您的输入有误!");
        }
    }

    //定义一个关于行为的方法
    private void show() {
        System.out.println("昵称:" + name + "健康值:" + healthValue + "亲密度:" + intimacy + "品种:" + gender);
    }
}

上机练习2——打印Dog信息2-1

需求说明:
            – 根据控制台提示信息选择领养宠物(狗),
            ▪ 输入昵称、品种、健康值
            ▪ 打印宠物信息
– 要保证健康值的有效性(在1到100之间
package wangKe.zuoYe01;

import java.util.Scanner;

public class PetsDemo {
    //main方法
    public static void main(String[] args) {
        System.out.println("欢迎来到宠物店");

        //创建一个接收键盘录入的对象
        Scanner sc=new Scanner(System.in);

        System.out.print("请选择您要领养的宠物类型(1.小狗  2.企鹅):");
        int type =sc.nextInt();
        if (type !=1 && type !=2){
            type =1;
            System.out.println("您的输入有误!");
        }
        switch (type){
            case 1:
                System.out.print("为您的小狗起个名字吧:");
                String name =sc.next();
                System.out.print("请选择您想要的品种:");
                String varieties=sc.next();
                System.out.print("您宠物的健康值为:");
                int healthValue =sc.nextInt();
                //判断
                if (healthValue<=0 || healthValue>100){
                    System.out.println("您的输入有误!已为您设置默认值:80");
                }else {
                    healthValue =healthValue;
                }
                System.out.println("您的宠物是一只小狗"+"\t"+"它的名字是:"+name +"\t"+"它的品种是:"+varieties+"\t"+"它的健康值为:"+healthValue);
                break;
            case 2:
                System.out.print("为您的企鹅起个名字吧:");
                String name1 =sc.next();
                System.out.print("请选择您想要的性别:");
                String gender=sc.next();
                System.out.print("您宠物的健康值为:");
                int healthValue1 =sc.nextInt();
                if (healthValue1<=0 || healthValue1>100){
                    System.out.println("您的输入有误!已为您设置默认值:80");
                }else {
                   healthValue1 =healthValue1;
                }
                System.out.println("您的宠物是一只企鹅"+"\t"+"它的名字是:"+name1 +"\t"+"它的性别是:"+gender+"\t"+"它的健康值为:"+healthValue1);
                break;
        }
    }
}

上机练习3——Dog类的带参构造方法

需求说明:
– 增加带参构造方法
Dog(String name, String strain)
– 修改PetsDemo类,使用带参构造方法创建对象
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值