【九日目】Java学習

公開日: 2022/12/19 更新日: 2022/12/19
Java学習9日目

1. 練習問題


class Parent {
int num = 100;
void show() {
System.out.println(num);
}
}
class Child extends Parent {
int num = 500;
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.show();
}
}

実行結果:java Main
100


public class Main {
public static void main(String[] args) {
A obj1 = new B();
B obj2 = new C();
D obj3 = new D();
if(obj1 instanceof A){ System.out.print("obj1 "); }
if(obj2 instanceof A){ System.out.print("obj2 "); }
if(obj3 instanceof A){ System.out.print("obj3 "); }
}
}

interface A { }
class B implements A { }
class C extends B { }
class D { }

実行結果:java Main.java
obj1 obj2


public class Main {
int x;
int y;
void methodA(int x, int y) {
x = x;
y = y;
}
void methodB(int x, int y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
Main obj = new Main();
obj.methodA(10,20);
System.out.println("methodA:" + obj.x + " " + obj.y);
obj.methodB(50,60);
System.out.println("methodB:" + obj.x + " " + obj.y);
}
}

実行結果:java Main.java
methodA:0 0
methodB:50 60