class Dog {
public Dog() {
age = 1;
color = "Black";
System.out.println("Default Constructor Dog");
}
public Dog(int ag, String clr) {
age = ag;
color = clr;
}
String breed;
int age;
String color;
void barking() {}
int hungry() { int a = 1; int b = 2; int c = a + b; return c;}
void steeping() {}
}
public class Test1 {
public static void main(String[] args) {
//Dog d = new Dog();
Dog d2 = new Dog(2, "땡칠이");
}
}
/////////////////////////////////////////////////////////
public class Test2 {
public static void main(String[] args) {
String str = new String("test");
String str1 = new String("test");
String str2 = "test";
String str3 = "test";
if(str == str1) {
System.out.println("Ok");
}
if(str.equals(str1)) {
System.out.println("Ok2");
}
if(str2 == str3) {
System.out.println("Ok3");
}
}
}
/////////////////////////////////////////////////////////
class Calculator {
int result;
public Calculator() {
result = 0;
}
public Calculator(int r) {
result = r;
}
int add(int a, int b) {
result = a + b;
return result;
}
int subtract(int a, int b) {
result = a - b;
return result;
}
// Method Overloding
int add(int a, int b, int c) {
result = a + b + c;
return result;
}
}
public class Test3 {
public static void main(String[] args) {
//Calculator calc = new Calculator();
Calculator calc = new Calculator(100);
int a = 1;
int b = 2;
int result = calc.add(a, b);
System.out.println(result);
}
}
/////////////////////////////////////////////////////////
class Student {
public void gotoSchool() {
System.out.println("Student go to school");
}
public void study() {
System.out.println("Student study");
}
}
class MiddleStudent extends Student {
public void study2() {
System.out.println("MiddleStudent study");
}
}
public class Test4 {
public static void main(String[] args) {
MiddleStudent stu = new MiddleStudent();
stu.gotoSchool();
stu.study2();
}
}
/////////////////////////////////////////////////////////
class Student2 {
String name;
int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
public class Test5 {
public static void main(String[] args) {
Student2 stu = new Student2();
stu.setName("홍길동");
}
}
/////////////////////////////////////////////////////////
class A {
int aValue;
public A() {
System.out.println("Default Constructor A");
this.aValue = 10;
}
public A(int aValue) {
System.out.println("Constructor A");
this.aValue = aValue;
}
}
class B extends A {
int bValue;
public B() {
System.out.println("Default Constructor B");
this.bValue = 100;
}
public B(int bValue) {
super(bValue);
this.bValue = bValue;
System.out.println("Constructor B " + bValue + ", " + this.aValue);
}
}
public class Test6 {
public static void main(String[] args) {
B b = new B(100);
}
}
/////////////////////////////////////////////////////////
class Student4 {
String name;
public Student4(String name) {
this.name = name;
}
}
class MiddleStudent4 extends Student4 {
int age;
public MiddleStudent4(String name, int age) {
super(name);
this.age = age;
System.out.println(name + ", " + age);
}
}
public class Test7 {
public static void main(String[] args) {
MiddleStudent4 stu =
new MiddleStudent4("홍길동", 20);
}
}
/////////////////////////////////////////////////////////
class MyClass{
//int count;
static int count;
public MyClass() {
count++;
System.out.println(count);
}
}
public class Test8 {
public static void main(String[] args) {
MyClass myClass = new MyClass();
MyClass myClass2 = new MyClass();
MyClass myClass3 = new MyClass();
}
}
/////////////////////////////////////////////////////////
class MyClass3 {
int a;
static int b;
public static void setAValue(int x) {
b = x;
}
public void setBValue(int y) {
a = y;
setAValue(10);
}
}
public class Test9 {
public static void main(String[] args) {
MyClass3.b = 20;
MyClass3 m = new MyClass3();
m.a = 10;
m.b = 20;
}
}
/////////////////////////////////////////////////////////
class MyClass4 {
static int a;
static int b;
static {
a = 1;
b = 2;
System.out.println("Static Initialize");
}
}
public class Test10 {
public static void main(String[] args) {
MyClass4 m = new MyClass4();
}
}
/////////////////////////////////////////////////////////
class Student5 {
String name;
int age;
public Student5(String name, int age) {
this.name = name;
this.age = age;
}
}
class HighStudent5 extends Student5 {
int grade;
public HighStudent5(String name, int age, int grade) {
super(name, age);
this.grade = grade;
}
void setNameAge(String name, int age) {
this.name = name;
this.age = age;
}
void study() {
System.out.println(name + ", " + age + ", " + grade);
}
}
public class Test11 {
public static void main(String[] args) {
HighStudent5 stu = new HighStudent5("홍길동", 20, 1);
}
}
/////////////////////////////////////////////////////////
class A2 {
void aMethod() {System.out.println("aMethod");}
}
class B2 extends A2 {
void bMethod() {System.out.println("bMethod");}
void aMethod() {System.out.println("aMethod on B2 Class");}
}
class C2 extends A2 {
void cMethod() {System.out.println("cMethod");}
void aMethod() {System.out.println("aMethod on C2 Class");}
}
public class Test12 {
static void callMethod(B2 b) {
b.aMethod();
}
static void callMethod(C2 c) {
c.aMethod();
}
static void callMethod2(A2 a) {
a.aMethod();
}
public static void main(String[] args) {
callMethod(new B2());
callMethod(new C2());
callMethod2(new B2());
callMethod2(new C2());
// B2 b = new B2();
// b.aMethod();
// b.bMethod();
// A2 a = b;
// a.aMethod();
// B2 b1 = (B2)a;
// b1.aMethod();
// b1.bMethod();
//
// C2 c = new C2();
// c.aMethod();
// c.cMethod();
// A2 a2 = c;
// a2.aMethod();
// C2 c2 = (C2)a2;
// c2.aMethod();
// c2.cMethod();
B2 b2 = new B2();
A2 a2 = b2;
a2.aMethod();
}
}
/////////////////////////////////////////////////////////
class Animal2 {
void move() { System.out.println("Animal move");}
}
class Dog2 extends Animal2 {
void move() { System.out.println("Dog move");}
}
class Cat2 extends Animal2 {
void move() { System.out.println("Cat move");}
}
public class Test13 {
static void goMove(Animal2 a) {
a.move();
}
public static void main(String[] args) {
Animal2 a = new Animal2();
a.move();
Dog2 d = new Dog2();
d.move();
Animal2 a2 = new Dog2();
}
}