错在哪里打到public goid print(){}就错了

来源:7-1 什么是 Java 中的封装

Jerrylinusa

2015-12-01 11:35

package eletricpet;


public class dog {

    private String name = "无名氏";

    private int health = 100;

    private int love = 0;

    private String strain = "聪明的拉布拉多";

    

    //给名字赋值

    public void setName(String name){

    this.name=name;

    }

    //取name值

    public String getName(){

    return this.name;  

    }

    //health

    public void setHealth(int health){

    if(health<=0 || health>100){

    System.out.println("健康有误");

    this.health=60;

    }else

    this.health=health;

    }

    //love

    public void setLove(int love){

    if(love<=0 || love>100){

    System.out.println("亲密度有误");

    this.love=3;

    }else

    this.love=health;

    //品种

     public void print(){

     

     }


写回答 关注

3回答

  • 高恒博
    2016-03-26 17:31:03
    已采纳

    package com.huawei.xuexi;


    import java.net.MalformedURLException;

    import java.net.URL;


    //关于url的常用方法

    public class Test02 {

       private String name = "无名氏";

       private int health = 100;

       private int love = 0;

       private String strain = "聪明的拉布拉多";

       

       //给名字赋值

       public void setName(String name){

       this.name=name;

       }

       //取name值

       public String getName(){

       return this.name;  

       }

       //health

       public void setHealth(int health){

       if(health<=0 || health>100){

       System.out.println("健康有误");

       this.health=60;

       }else

       this.health=health;

       }

       //love

       public void setLove(int love){

       if(love<=0 || love>100){

       System.out.println("亲密度有误");

       this.love=3;

       }else

       this.love=health;

       }

       //品种

        public void print(){

        

        }

    }


  • 敞篷帅哥
    2015-12-01 14:52:19

    少两个大括号,this.love=health;这行下面少一个,最后少一个。

  • sixGod
    2015-12-01 11:59:35

      //love

        public void setLove(int love){

        if(love<=0 || love>100){

        System.out.println("亲密度有误");

        this.love=3;

        }else

        this.love=health;

    这个方法是不是少一个大括号?

Java入门第二季

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

531531 学习 · 6329 问题

查看课程

相似问题