package england.people;
import american.people.Son;
import japan.people.Grandson;
public class Ex05_1 {
public static void main(String[] args) {
Son son=new Son();
Grangson grandson= new Grangson();
son.height = 180;
son.hand = "一双大手";
grandson.height = 155;
grandson.hand = "一双小手";
grandson.foot = "一双小脚";
String str = son.getHand();
System.out.println("儿子:%s,%d\n",str,son.height);
str = grandson.getHand();
System.out.println("孙子:%s,%s,%d\n",str,grandson.foot,grandson.height);
}
}
class people{
public double x;
public void setX(double x) {
this.x = x;
}
public double getDoubleX() {
return x;
}
}
class Student extends People{
int x;
public int getX() {
//x=20.56 //非法,因为子类的x已经是int型,不是double型
return x;
}
}
public class Ex05_3 {
public static void main(String[] args) {
Student stu=new Student();
stu.x=98; //合法,子类对象的x是int型
System.out.println("对象stu的x的值是:"+stu.getX());
//stu.x=98.98 //非法,因为子类的对象已经是int型
stu.setX(98.98); //子类对象调用继承的方法操作隐藏的double型变量x
double m=stu.getDouble(); //子类对象调用继承的方法操作隐藏的double型变量x
System.out.println("对象stu隐藏的x的值是:"+m);
}
}
class A{
double f(float x,float y) {
return x+y;
}
public int g( int x, intint y){
return x+y;
}
}
class B extends A{
double f(float x,float y){ {
return x*y;
}
}
public class Example5_4 {
public static void main(String args[]) {
B b=new B();
double result= b.f(5,6); //b调用重写的方法
System.out.println("调用重写方法得到的结果:" +result);
intm=b.g(3,5); //b调用继承的方法
System.out.println("调用继承方法得到的结果:"+m);
}
}
public class Example5_6{
public static void main(String args[]){
UniverStudent zhang = new UniverStudent(20111,"张三",false);
int number = zhang.getNumber();
String name = zhang. getName();
boolean marriage = zhang. getIsMarriage( );
System.out.println(name+"的学号是:"+number);
if(marriage ==true){
System.out.println(name +"已婚");
}
else{
System. out. println(name +"未婚");
}
}
}
public class Example5_7{
public static void main(String args[]) {
Average aver = new Average();
aver.n=100.5678;
double result1 = aver. f() ;
double result2 = aver. g( ) ;
System. out. println(1n("result1 = "+result1);
System. out.printlr1n("result2 = "+ result2);
}
}
public class Example5_9 {
public static void main(String args[]) {
People people = new People() ;
Anthropoid monkey = people; //monkey是people对象的上转型对象
monkey.crySpeak("I love this game"); //等同于 people调用重写的crySpeak 方法
//monkey.n=100; //非法,因为n是子类新增的成员变量
//monkey.computer(12,19); //非法,因为computer()是子类新增的方法
System.out.println(monkey.m); //操作隐藏的m,不等同于 people.m
System. out. println(people.m); //操作子类的m
People zhang = (People)monkey; //把上转型对象强制转化为子类的对象
zhang. computer(55,33); //zhang是子类的对象
zhang.m = 'T'; //操作子类声明的成员的变量m
System.out.println(zhang.m);
}
}
abstract class GirlFriend { //抽象类,封装了两个行为标准
abstract void speak() ;
abstract void cooking() ;
}
class ChinaGirlFriend extends GirlFriend {
void speak(){
System.out.println("你好");
}
void cooking(){
System.out.println("水煮鱼");
}
}
class AmericanGirlFriend extends GirlFriend {
void speak(){
System.out.println("hello");
}
void cooking(){
System.out. println("roast beef");
}
}
class Boy{
GirlFriend friend;
void setGirlfriend(GirlFriend f){
friend=f;
}
void showGirlFriend(){
friend.speak();
friend.cooking();
}
}
public class Example5_11{
public static void main(String args[ ]) {
GirlFriend girl = new ChinaGir1Friend(); //gir1是上转型对象
Boy boy = new Boy();
boy. setGirlfriend(gir1);
boy.showGirlFriend();
girl = new AmericanGirlFriend(); //gir1是上转型对象
boy.setGirlfriend(gir1);
boy.showGirlFriend();
}
}
public class Example5_13 {
public static void main(String args[]) {
Simulator simulator = new Simulator() ;
simulator. playSound(new Dog()) ;
simulator.playSound(new Cat());
}
}
import java.awt.print.Printable;
public class Ex05_14 {
public static void main(String[] args) {
AAA a = new AAA();
System.out.println("接口中的常量"+AAA.MAX);
System.out.println("调用on方法(重写的):");
a.on();
System.out.println("调用sum方法(重写的):"+a.sum(12,18));
System.out.println("调用接口提供的default方法"+a.max(12,78));
Printable.f();
}
}
class A{
public final void f(){
char cStart = 'a' ,cEnd = 'z' ;
for(char c = cStart;c<= cEnd;c++) {
System.out.print(" "+c);
}
}
}
class B extends A {
public void g() {
char cStart = 'α' ,cEnd = 'ω';
for(char c = cStart;c<= cEnd;c++) {
System.out.print(""+c);
}
}
}
public class E{
public static void main (String args[ ]) {
B b = new B();
b.f();
b.g();
}
}
class A
{
//求最小公约数
public int f(int a,int b)
{
int max,min,r;
if(a<b)
{
max = b;
min = a;
}
else
{
max = a;
min = b;
}
while((r=max%min)!=0)
{
max = min;
min = r;
}
return min;
}
}
class B extends A
{
public int f(int a,int b)
{
int m = super.f(a,b);
return (a*b)/m;
}
}
class C
{
public static void main(String[] args)
{
A a = new A();
System.out.println("最大公约数="+a.f(4,6));
B b = new B();
System.out.println("最小公倍数="+b.f(4,6));
}
}