重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public class Monkey {
成都创新互联公司长期为1000多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为西吉企业提供专业的成都网站建设、网站制作,西吉网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
private String color;
private String sex;
public Monkey(String color,String sex) {
this.color = color;
this.sex = sex;
}
public Monkey(String color) {
this.color = color;
this.sex = "公";
}
public Monkey(){}
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 int add(int p1,int p2,int p3) {
return p1+p2+p3;
}
public int add(int p1,int p2) {
return p1+p2;
}
}
public class Zoo {
public static void main(String[] args) {
Monkey g = new Monkey("黑");
Monkey l = new Monkey("红","母");
int p1 = g.add(1, 2);
int p2 = l.add(1, 2, 3);
System.out.println(g.getSex()+":"+p1);
System.out.println(l.getSex()+":"+p2);
}
}
public class Animal
{
public String name;
public int legs;
public String sound;
public Animal(String name, int legs, String sound){
this.name = name;
this.legs = legs;
this.sound = sound;
}
public String toString(){
return name + "\t" + legs + "\t" + sound;
}
public static void main(String[]args)
{
//init
Animal[] animals = new Animal[3];
animals[0] = new Animal("加菲猫",4, "喵喵喵");
animals[1] = new Animal("唐小鸭",2, "嘎嘎嘎");
animals[2] = new Animal("海豚奇奇",0, "海豚音");
byte[] b = new byte[1024];
String[] names = {"猫", "鸭子", "海豚"};
int[] legs = {4,2,0};
String name;
int leg;
String sound;
while(true){
try{
System.out.println("动物名称\t腿的条数\t动物叫");
for (int i = 0; i animals.length; i++) {
System.out.println(animals[i].toString());
}
System.out.println("0-修改,其他数字-退出");
int n = System.in.read(b);
String s = new String(b,0,n);
int oper = Integer.parseInt(s.trim());
if (oper != 0) {
System.out.println("退出!");
return;
}
for (int i = 0; i animals.length; i++) {
System.out.println("请输入"+names[i]+"的名字");
n = System.in.read(b);
s = new String(b,0,n);
name = s.trim();
if (name.length() == 0) {
throw new Exception(names[i]+"必须有名字");
}
System.out.println("请输入"+names[i]+"的腿数");
n = System.in.read(b);
s = new String(b,0,n);
leg = Integer.parseInt(s.trim());
if (leg != legs[i]) {
throw new Exception(names[i]+"必须有"+legs[i]+"条腿");
}
System.out.println("请输入"+names[i]+"的叫声");
n = System.in.read(b);
s = new String(b,0,n);
sound = s.trim();
if (sound.length() == 0) {
throw new Exception(names[i]+"必须有声音");
}
animals[i].name = name;
animals[i].legs = leg;
animals[i].sound = sound;
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
凑合写的....
instanceof 不难吧...就是验证类是否为类或者父类的实例...
我看你ppt要求的是自定义异常抛出,接口继承应该很简单,抛出异常参考我的代码即可..
应用程序什么时候结束取决于你一开始创建了多少动物???
从你的描述看来,无论有多少只动物,最长命的动物的寿命即是程序的寿命了。