Java学習10日目
【十日目】Java学習
1. 練習問題
class Animal {
public static void saySomething() {
System.out.print("Gurrr! ");
}
}
public class Cow extends Animal {
public static void saySomething() {
System.out.print("Moo! ");
}
public static void main(String[] args) {
Animal[] animals = {new Animal(), new Cow()};
for(Animal a : animals) {
a.saySomething();
}
}
}
実行結果:java Cow
Gurrr! Gurrr!
class Animal {
void saySomething() {
System.out.print("Gurrr! ");
}
}
public class Cow extends Animal {
protected void saySomething() {
System.out.print("Moo! ");
}
public static void main(String[] args) {
Animal[] animals = {new Animal(), new Cow()};
for(Animal a : animals) {
a.saySomething();
}
}
}
実行結果:java Cow
Gurrr! Moo!
class Animal {
static void x() {
System.out.print("Animal ");}
void y() { x(); }
}
class Cow extends Animal {
static void x() {
System.out.print("Cow ");}
void z() { x(); }
}
public class Main {
public static void main(String[] args) {
Cow obj = new Cow();
obj.y();
obj.z();
}
}
実行結果:>java Main
Animal Cow
class Foo {
void method(Object obj) {System.out.println("Object");}
void method(Number obj) {System.out.println("Number");}
void method(Integer obj) {System.out.println("Integer");}
void method(char[] obj) {System.out.println("char[] ");}
}
public class Main {
public static void main(String[] args) {
Foo foo = new Foo();
foo.method("Hello");
}
}
実行結果:java Main
Object
import java.util.*;