개발/K-DigitalTraining 수강중
3/2 자바 수업 (상속)
배타브
2023. 3. 2. 18:02
728x90
반응형
1교시
작성한 클래스
package ch6;
public class Student2 {
//학생이름 name
//반 ban
//번호 no
//국어점수 kor
//영어점수 eng
//수학점수 math
String name;
int ban;
int no;
int kor;
int eng;
int math;
public Student2(int kor, int eng, int math) {
super();
this.kor = kor;
this.eng = eng;
this.math = math;
}
public Student2(String name, int ban, int no) {
super();
this.name = name;
this.ban = ban;
this.no = no;
}
public Student2(String name, int ban, int no,
int kor,int eng, int math) {
super();
this.name = name;
this.ban = ban;
this.no = no;
this.kor = kor;
this.eng = eng;
this.math = math;
}
int getTotal() {
return this.kor+this.eng+this.math;
}
double getAverage() {
return getTotal() / 3.0;
}
public Student2(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "Student2 [name=" + name + ", ban=" + ban + ", no=" + no + ", kor=" + kor + ", eng=" + eng + ", math="
+ math + "]";
}
}
를 이용해 학생정보 작성하기
package ch6;
import java.util.Scanner;
public class StudentEx3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("이름: ");
String name = sc.nextLine();
System.out.println("반 : ");
int ban = Integer.parseInt(sc.nextLine());
System.out.println("번호 : ");
int no = Integer.parseInt(sc.nextLine());
System.out.println("국어 : ");
int kor = Integer.parseInt(sc.nextLine());
System.out.println("영어 : ");
int eng = Integer.parseInt(sc.nextLine());
System.out.println("수학 : ");
int math = Integer.parseInt(sc.nextLine());
Student2 student = new Student2(name, ban, no, kor, eng, math);
System.out.println(student);
}
}
2교시
package ch6;
//상품 관리
public class GoodsStock {
//속성
String code; //상품코드
int stockNum; //재고수량
//생성자를 명시하지 않으면
//기본생성자를 생성해줌
public GoodsStock(String code, int stockNum) {
super();
this.code = code;
this.stockNum = stockNum;
}
//기능
//재고수량 감소
void addStock() {
System.out.println("재고수량 증가");
}
//재고수량 증가
void subtractStock() {
System.out.println("재고수량 감소");
}
@Override
public String toString() {
return "GoodsStock [code=" + code + ", stockNum=" + stockNum + "]";
}
}
package ch6;
import java.util.Scanner;
public class GoodStockEx2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// System.out.println("코드를 입력해주세요>>");
// String code = sc.nextLine();
// System.out.println("재고수량 입력해주세요>>");
// int stockNum =Integer.parseInt(sc.nextLine());
//
// GoodsStock stock = new GoodsStock(code, stockNum);
// System.out.println(stock);
//3개의 상품에 대해서 인스턴스를 생성해야 한다면?
GoodsStock[] goods = new GoodsStock[3];
for (int i = 0; i < goods.length; i++) {
System.out.println("코드를 입력해주세요>>");
String code = sc.nextLine();
System.out.println("재고수량 입력해주세요>>");
int stockNum =Integer.parseInt(sc.nextLine());
goods[i] = new GoodsStock(code, stockNum);
System.out.println("-----------------------");
}
for (int i = 0; i < goods.length; i++) {
System.out.println(goods[i]);
}
}
}
며칠째 같은내용을 반복해서 하니 졸리다...
기초가 중요하다곤 하지만 이렇게 공부하는게 맞는걸까..? 하면할수록 그냥 귀찮다는 생각밖에 안든다.
3교시
/* 상속
* 기존의 클래스를 재사용하여 새로운 클래스를 작성
* 상성 장점
* 1)적은 양의 코드로 새로운 클래스를 작성할 수 있음
* 2)코드를 공통적으로 관리하기 때문에 추가 및 변경이 용이함
*
* extends : 상속을 표현
*
* 상속대상
* 1)자손 클래스는 조상 클래스의 모든 멤버를 상송받음 (단, 생성자와 초기화 블럭은 상속 안됨)
* 2)자손 클래스의 멤버 개수는 조상 클래스보다 항상 같거나 많음-
*/
4교시
package inheritense;
public class Tv {
boolean power;
int channel;
void power() {
power =! power;
}
void channelUp() {
channel ++;
}
void channelDown() {
channel --;
}
}
package inheritense;
//Tv 클래스를 상속받은 SmartTv
public class SmartTv extends Tv {
boolean caption;
void displayCaption(String text) {
System.out.println(text);
}
}
package inheritense;
public class SmartTvEx1 {
public static void main(String[] args) {
SmartTv stv = new SmartTv();
//상속여부 확인
stv.channel = 10;
stv.channelUp();
System.out.println("현재채널" + stv.channel);
stv.power();
System.out.println("전원 상태 "+ stv.power);
stv.displayCaption("Hello world");
stv.caption = true;
stv.displayCaption("hello world");
}
}
5교시
//this() : 생성자에서 다른 생성자를 호출시
//, this.변수명 : 자기자신
//super() : 생성자에서 부모 생성자를 호출 시
//, super.변수명 : 부모
6교시
클래스 작성시 부모를 지정하지 않으면 최상위 부모는 Object 클래스가 존재함
package Exam;
public class Account {
private String ano;
private String owner;
private String balance;
public Account(String ano, String owner, String balance) {
super();
this.ano = ano;
this.owner = owner;
this.balance = balance;
}
public String getAno() {
return ano;
}
public void setAno(String ano) {
this.ano = ano;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getBalance() {
return balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
}
Account 클래스 생성
6교시
7교시
8교시
9교시
BankApp 클래스 작성중
복잡하다 복잡해 @@
728x90
반응형