728x90
반응형
/* 01 - Reference */
package package_01;
public class TestClass{
private int a;
public void set(int a) {
this.a = a;
}
public void add(int d) {
a += d;
}
public void print() {
System.out.println(a);
}
public static void main(String args[]) {
TestClass p = new TestClass();
TestClass q;
p.set(10);
q = p;
p.add(10);
q.set(30);
p.print();
}
}
/* 02 - Constructor */
package package_01;
class Ref{
int a;
Ref(int x){
a = x;
}
int sum(Ref obj) {
int k;
k = obj.a - a;
a = 10;
obj.a = 20;
return k;
}
}
class TestClass{
public static void main(String args[]) {
Ref obj1 = new Ref(3);
Ref obj2 = new Ref(4);
int k1 = obj2.sum(obj1);
System.out.print(" k1 = " +k1);
System.out.print(" obj1.a= " +obj1.a);
System.out.print(" obj2.a= " +obj2.a);
}
}
// k1 = -1 obj1.a= 20 obj2.a= 10
/* 03 - Access Modifier */
package package_01;
class Person{
private String name;
public int age;
public void setAge(int age) {
this.age = age;
}
public String toString() {
return("name: " + this.name + ", age: " + this .age);
}
}
class TestClass{
public static void main(String[] args) {
Person a = new Person();
a.setAge(27);
// a.name = "Jake";
System.out.println(a);
}
}
//name: null, age: 27
/* 04- Inheritance */
class C {}
class CS extends C {}
interface I {}
class CI extends C implements I {}
public class TestClass{
public static void main(String args[]) {
static I i = new CI();
static C ca = new CI();
// static CS cs = new C();
static C cb = new CS();
}
}
/*
* C - CS - static C cb
* - CI - static I i
* - static C ca
* ㄴ static CS cs (x)
*/
/* 05- Access, Inheritance */
class A1{
public int x;
private int y;
protected int z;
}
class A2 extends A1{
protected int a;
private int b;
}
class A3 extends A2{
private int q;
// x, z, a, q
}
/* 06 - Inheritance */
class Subject1{
protected int a = 1000;
public int fun1() {
return a;
}
}
class Subject2 extends Subject1{
private int b = 5;
public int fun2() {
return a/b;
}
}
public class TestClass{
public static void main(String[] args) {
Subject2 sub = new Subject2();
System.out.println(sub.fun1());
System.out.println(sub.fun2());
}
}
//1000
//200
/* 07 - Inheritance, super() */
class A{
A(){
System.out.printf("%d ", 10);
}
}
class B extends A{
B(int a){
System.out.printf("%d ", a);
}
}
class C extends B{
C(int a){
super(a/10);
System.out.printf("%d ", a);
}
}
public class TestClass{
public static void main(String[] args) {
A b = new C(1000);
}
}
// 10 100 1000
/* 08 - Inheritance, Constructor, super() */
class A{
int a;
A(int a){
this.a = a; // Constructor
}
void display() {
System.out.println("a="+a);
}
}
class B extends A{
B(int a){ // Constructor
super(a);
super.display();
}
}
public class TestClass{
public static void main(String[] args) {
B obj = new B(10);
}
}
// a = 10
/* 09 - Inheritance, super() */
class AA{
int d1;
int s;
AA(int s1){
s = s1;
d1 = s * s;
}
}
class BB extends AA{
int d2;
int t;
BB(int s1, int t1){
super(s1);
t = t1;
d2 = t * t;
}
}
class TestClass{
public static void main(String[] args) {
BB myTest = new BB(10, 20);
System.out.println("Result1: " + myTest.d1);
System.out.println("Result2: " + myTest.d2);
}
}
//Result1: 100
//Result2: 400
/* 10 - Inheritance, super() */
class Super{
Super(){
System.out.print('A'); // 4 (A)
}
Super(char x){
System.out.print(x); // 1 (C)
}
}
class Sub extends Super{
Sub(){
super(); // 3
System.out.print('B'); // 5 (B)
}
Sub(char x){
this(); // 2
System.out.print(x); // 6 (D)
}
}
class TestClass{
public static void main(String[] args) {
Super s1 = new Super('C');
Super s2 = new Sub('D');
}
}
// CABD
/* 11 - Inheritance, constructor */
class TestClass{
int a = 10;
public TestClass() {
System.out.print("가");
}
public TestClass(int x) {
System.out.print("나");
}
public static void main(String[] args) {
B b1 = new B();
TestClass b2 = new B(1);
System.out.print(b1.a + b2.a);
}
}
class B extends TestClass{
int a = 20;
public B() {
System.out.print("다");
}
public B(int x) {
System.out.print("라");
}
}
// 가다가라30
/* 12 - Overloading */
class TestClass{
void f() {
System.out.println("0");
}
void f(int i) {
System.out.println(i);
}
void f(int i, int j) {
System.out.println(i+j);
}
public static void main(String[] args) {
TestClass a = new TestClass();
a.f(25, 25);
}
}
// 50
728x90
반응형
'Language > Java' 카테고리의 다른 글
Singleton Pattern (0) | 2023.04.14 |
---|---|
Java - super, super() (0) | 2023.04.13 |
Java - Exception (0) | 2023.03.27 |
Java - Static (0) | 2023.03.27 |
Java - Access Modifier (0) | 2023.03.27 |