interface Animal{
int ID=1;
void breathe();
default void getType(String type){
System.out.println("该动物属于:"+type);
}
static int getID(){
return Animal.ID;
}
}
class Dog implements Animal{
public void breathe(){
System.out.println("狗在呼吸");
}
}
public class Example1{
public static void main(String args[]){
System,out.pritnln(Animal.getID());
Dog dog=new Dog();
System.out.println(dog.ID);
dog.breathe();
dog.getType("犬科");
}
}
运行结果:
1
1
狗在呼吸
该动物属于:犬科
public class HelloWorld {
public static void main(String []args) {
f f1= new g();
f1.s();
}
}
interface f{
public void s();
}
class g implements f{
public void s(){
System.out.println("tyu");
}
}
运行结果:
tyu
改成如下代码是报错
public class HelloWorld {
public static void main(String []args) {
f f1= new f() {
@Override
public void s() {
System.out.println("1");
}
};
f1.s();
}
}
interface f{
public void s();
}
class g implements f{
public void s(){
System.out.println("tyu");
}
}
运行结果:
1
interface s{
public void g();
}
class d implements s{
public void g(){
System.out.println("1");
}
}
public class HelloWorld{
s s1=new d();
public void ghj(){
s1.g();
}
public static void main(String[] args) {
HelloWorld helloWorld=new HelloWorld();
helloWorld.ghj();
}
}
运行结果:
1
interface s{
public void g();
}
class d implements s{
private static d s1=new d();
public void g(){
System.out.println("1");
}
public static void fgh(){
s1.g();
}
}
public class HelloWorld{
public static void main(String[] args) {
d.fgh();
System.out.println("16890");
}
}
运行结果:
1
16890
interface s{
public void g();
}
class d implements s{
static s s1=new d();
public void g(){
System.out.println("156");
}
public static void k(){
s1.g();
}
}
public class HelloWorld{
public static void main(String[] args) {
d.k();
System.out.println("16890");
}
}
运行结果:
156
16890
interface s{
public void g();
}
class d implements s{
s s1= new s() {
@Override
public void g() {
System.out.println("1569");
}
};
public void g(){
System.out.println("156");
}
public void k(){
s1.g();
}
}
public class HelloWorld{
public static void main(String[] args) {
d d1=new d();
d1.k();
System.out.println("16890");
}
}
运行结果:
1569
16890