1번 - 별찍기
package sevenClass;
import java.util.Scanner;
public class Test0516_01 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("단을 입력>>");
int n = scan.nextInt();
for(int i = 1; i <= n; i++) {
for(int j = n; j > i; j--) {
System.out.print(" ");
}
for(int k = 1; k <= (i*2-1); k++) {
System.out.print("*");
}
System.out.println();
}
}
}
결과
단을 입력>>10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
2번 - 오버 라이딩
package sevenClass;
public class Test0516_02 {
public static void main(String[] args) {
// Truck t = new Truck();
// t.speedUp();
// t.speedDown();
// t.name = "프론티어";
// //업캐스팅
// //부모클래스 = 자식클래스
// //다운캐스팅
// //업캐스팅한 객체를 다시 원래의 형태로 돌릴때
// Car c = new Truck(); //업캐스팅
// c.speedUp();
// Truck t1 = (Truck)c; //다운캐스팅
// t1.speedUp();
Car c1, c2;
Bus b1, b2;
Truck t1, t2;
c1 = new Car();
b1 = new Bus();
t1 = new Truck();
System.out.println("[" + c1.print() + "]");
System.out.println("[" + b1.print() + "]");
System.out.println("[" + t1.print() + "]");
//업캐스팅
c2 = b1;
//다운캐스팅
b2 = (Bus)c2;
System.out.println("[" + b2.print() + "]");
//업캐스팅
c2 = t1;
//다운캐스팅
t2 = (Truck)c2;
System.out.println("[" + t2.print() + "]");
}
}
class Car{
String name = "차";
String print() {
return "차의 종류는 " + this.name;
}
int velocity;
void speedUp(){
velocity += 50;
System.out.println("속도 " + velocity + "증가");
}
void speedDown() {
velocity -= 50;
System.out.println("속도 " + velocity + "감소");
}
}
class Truck extends Car{
String name = "트럭";
String print() {
return "나는 " + this.name;
}
int t = 5;
void speedUp() {
velocity += 10;
System.out.println("속도 " + velocity + "증가");
}
void speedDown() {
velocity -= 10;
System.out.println("속도 " + velocity + "감소");
}
}
class Bus extends Car{
String name = "버스";
String print() {
return "차의 종류는 "+this.name;
}
}
결과
[차의 종류는 차]
[차의 종류는 버스]
[나는 트럭]
[차의 종류는 버스]
[나는 트럭]
3번 - 업캐스팅
package sevenClass;
public class Test0516_03 {
public static void main(String[] args) {
Ship ship1, ship2;
MyShip myship1, myship2;
YourShip yourship1, yourship2;
//업캐스팅
ship1 = new Ship();
myship1 = new MyShip();
yourship1 = new YourShip();
//업캐스팅
ship2 = myship1;
//다운캐스팅
myship2 = (MyShip)ship2;
//업캐스팅
ship2 = myship1;
//다운캐스팅
myship2 = (MyShip)ship2;
}
}
class Ship{
String name() {return "배이름";}
}
class MyShip extends Ship{
String name() {return "타이타닉호";}
}
class YourShip extends Ship{
String name() {return "해적선";}
}
2024.05.09 - [자바] - JAVA - 0509
JAVA - 0509
1번 - 택시package sevenClass;public class Test0509_01 { public static void main(String[] args) { Taxi mytaxi = new Taxi("1"); }}class Car{ Car(){} Car(String name){ System.out.println("이름이 있는 카 생성자"); }}class Taxi extends Car{ Taxi(){
conewbie.tistory.com
'자바' 카테고리의 다른 글
JAVA - 0509 (0) | 2024.05.09 |
---|---|
JAVA-0502 (0) | 2024.05.02 |
JAVA - 0422 (0) | 2024.04.22 |
JAVA - 0415 (0) | 2024.04.15 |
JAVA - 0411 (0) | 2024.04.11 |