package V;public class TestAnimal { public static void main(String[] args) { Animla a1=new Animla(); a1.name="花花"; a1.legs=-4;//这里赋值应该有限制,腿不应该是负数,赋值有点宽泛 a1.info(); a1.eat(); }}class Animla { String name; int legs; public void eat() { System.out.println("动物进食"); } public void sleep() { System.out.println("动物睡觉"); } public void info() { System.out.println("name:" + name + " legs:" + legs); }}
package V;public class TestAnimal { public static void main(String[] args) { Animla a1=new Animla();a1.setLegs(-4); a1.setName("小花"); a1.info(); a1.eat(); }}class Animla { String name; int legs; public void eat() { System.out.println("动物进食"); } public void sleep() { System.out.println("动物睡觉"); } public void info() { System.out.println("name:" + name + " legs:" + legs); } public void setLegs(int l){ if (l>0 && l%2==0){ legs=l; }else{ System.out.println("输入数据有误"); } } public void setName(String n){ name=n; }}
package V;public class TestAnimal { public static void main(String[] args) { Animla a1=new Animla(); a1.name="花花"; a1.legs=-4;//这里赋值应该有限制,腿不应该是负数,赋值有点宽泛 //a1.setLegs(-4); //a1.setName("小花"); a1.info(); a1.eat(); }}class Animla { //private 修饰的属性只能在本类中被调用,出了此类就不能被调用了 private String name; private int legs; public void eat() { System.out.println("动物进食"); } public void sleep() { System.out.println("动物睡觉"); } public void info() { System.out.println("name:" + name + " legs:" + legs); } public void setLegs(int l){ if (l>0 && l%2==0){ legs=l; }else{ System.out.println("输入数据有误"); } } public void setName(String n){ name=n; }}
public void setLegs(int l){ if (l>0 && l%2==0){ legs=l; }else{ System.out.println("输入数据有误"); } } public int getLegs(){ return legs; }