1.
(1)
public class dog { public void eat(){ System.out.println("在吃狗粮"); } public void drink(){ System.out.println("在喝水"); } public void lookhome(){ System.out.println("在看家"); } }
(2)
public class hashiqi extends dog{ public void chaijia(){ System.out.println("哈士奇在拆家"); } }
(3)
public class shapigou extends dog { @Override public void eat() { super.eat();//吃狗粮 System.out.println("在吃骨头"); } }
(4)
public class chinesedog extends dog{ @Override public void eat() { System.out.println("在吃剩饭"); } }
(5)
public class text { public static void main(String[] args) { hashiqi h=new hashiqi(); h.eat(); h.drink(); h.chaijia(); h.lookhome(); System.out.println(); chinesedog cd=new chinesedog(); cd.eat(); cd.drink(); cd.lookhome(); } }
2.
/*struct peo
{
char name[20];
char tele[20];
char sex[5];
int high;
}p1, p2;*/ //p1,p2是全局结构体变量
struct peo
{
char name[20];
char tele[20];
char sex[5];
int high;
};//也可以在main函数里,不过只会在main函数里有效
//结构体创建可以在任何地方
struct st
{
struct peo p;//p是成员变量
int num;
float f;
};
void print1(struct peo p)
{
printf("%s %s %s %d\n", p.name, p.tele, p.sex, p.high);//结构体变量,成员变量
}
void print2(struct peo* sp)
{
printf("%s %s %s %d\n", sp->name, sp->tele, sp->sex, sp->high);//结构体指针->成员变量
}
int main()
{
struct peo p1 = {"liujiawei","1000007451","男",185};
struct st s = {{"fanwenshuo","272352","女",173},18,36.6f };//浮点数在内存中不能精确保存
printf("%s %s %s %d\n",p1.name, p1.sex,p1.tele,p1.high);
printf("%s %s %s %d %d %f\n", s.p.name, s.p.sex,s.p.tele,s.p.high,s.num,s.f);
print1(p1);
print2(&p1);
return 0;
}