728x90
반응형
(22.7.31)
# 21강 인스턴스 변수와 클래스 변수
인스턴스 변수 - 객체마다 갖는 고유한 변수
- 인스턴스를 생성할 때 만들어지며, 각각의 인스턴스 마다 자신의 값을 갖음
- 각 인스턴스마다 다른 값을 가져야 할 때 생성
- 인스턴스 생성 시 메모리에 생성
클래스 변수 - 모든 객체가 동일한 값을 갖는 변수
- 모든 인스턴스가 같은 저장공간을 공유
- 클래스가 처음 메모리에 로딩될 때 생성
- 인스턴스 없이 사용 가능
package day21;
public class Day21_1 {
public static void main(String[] args) {
Member member1 = new Member();
Member member2 = new Member();
Member member3 = new Member();
Member member4 = new Member();
member1.centerName = "Hello";
member1.name = "김철수";
member1.age =32;
member1.gender = "남자";
member2.centerName = "Hello";
member2.name = "김철순";
member2.age =32;
member2.gender = "여자";
member3.centerName = "Hello";
member3.name = "김철삼";
member3.age =43;
member3.gender = "남자";
member4.centerName = "Hello";
member4.name = "김철후";
member4.age =22;
member4.gender = "여자";
member1.centerName = "Bye";
System.out.println("member1----------");
member1.info();
System.out.println("member2----------");
member2.info();
System.out.println("member3----------");
member3.info();
System.out.println("member4----------");
member4.info();
}
class Member{
static String centerName = "Hello";
String centerName;
String name;
int age;
String gender;
void info() {
System.out.println("Center Name : "+centerName);
System.out.println("Name : "+name);
System.out.println("Age : "+age);
System.out.println("Gender : "+gender);
}
}
}
package day21;
public class Day21_2 {
public static void main(String[] args) {
System.out.println(Car.wheel);
Car myCar1 = new Car();
System.out.println(myCar1.speed);
Car myCar2 = new Car();
System.out.println("변경전");
System.out.println("myCar1.speed :"+myCar1.speed);
System.out.println("myCar2.speed :"+myCar2.speed);
System.out.println("myCar1.wheel :"+myCar1.wheel);
System.out.println("myCar2.wheel :"+myCar2.wheel);
myCar2.speed =100;
myCar2.wheel=100;
System.out.println("변경후");
System.out.println("myCar1.speed :"+myCar1.speed);
System.out.println("myCar2.speed :"+myCar2.speed);
System.out.println("myCar1.wheel :"+myCar1.wheel);
System.out.println("myCar2.wheel :"+myCar2.wheel);
}
}
class Car{
static int wheel = 4;
int speed;
}
# 22강 객체 타입 배열
- 같은 클래스로 만들어진 변수들의 나열된 집합
- 같은 구조를 갖고 있으나 다른 객체 값을 저장
- 클래스명 객체배열명[][] = new 클래스명[크기];
package day22;
public class Day22_1 {
public static void main(String[] args) {
Aclass ar[] = new Aclass[3];
ar[0] = new Aclass();//동일한 이름의 메서드가 필요하다.
//클래스(); 을 통해 꼭 객체를 생성해야만 객체 내 필드 접근 가능
ar[0].x =100;
ar[0].f1();
//각각의 요소를 입력안함 배열만 만듬 - 여기까지는
System.out.println(ar[0].x);
}
}
class Aclass{
int x;//변수
void f1() {//메서드
System.out.println("f1()");
}
}
package day22;
public class Day22_2 {
public static void main(String[] args) {
Animal animals [] = new Animal[3];
for(int i =0;i<3;i++) {
animals[0] = new Animal();
}
animals[0].kind = "고양이";
animals[0].name = "고이";
animals[0].age = 1;
animals[1].kind = "개";
animals[1].name = "고미";
animals[1].age = 2;
animals[2].kind = "쥐";
animals[2].name = "쥥";
animals[2].age = 10;
for(int i=0;i<3;i++) {
animals[i].info();
}
}
class Animal{
String kind;
String name;
int age;
void info() {
System.out.println("kind :"+kind);
System.out.println("name :"+name);
System.out.println("age :"+age);
}
}
}
# 23강 생성자
- 기본 생성자 - 객체 생성 시 호출되어, 변수들을 초기화 하는 메서드
클래스와 이름이 같음, 리턴타입/반환 값 없음
- 매개변수 생성자 -
package day23;
public class Day23_1 {
public static void main(String[] args) {
Aclass a = new Aclass();
}
}
class Aclass{
//기본생성자
public Aclass(){
System.out.println("Aclass 기본생성자 호출()");
}
}
package day23;
public class Day23_2 {
public static void main(String[] args) {
Cellphone myphone = new Cellphone();
System.out.println(myphone.model);
}
}
class Cellphone{
String model = "Galaxy 8";
String color = "red";
int capacity = 60;
Cellphone(){
System.out.println("model" +model);
System.out.println("color" + color);
System.out.println("capacity" +capacity);
}
}
package day23;
public class Day23_3 {
public static void main(String[] args) {
Bclass b = new Bclass("가길동");
System.out.println(b.name);
}
}
class Bclass{
String name;
Bclass(String name2){//매개변수 생성자
System.out.println("Bclass 의 매개변수 생성");
this.name=name2;
}
}
package day23;
public class Day23_4 {
public static void main(String[] args) {
Iphone myphone1 = new Iphone();
Iphone myphone2 = new Iphone("iphone SE","white",70);
System.out.println(myphone1.capacity);
System.out.println(myphone2.capacity);
}
}
class Iphone{
String model;
String color;
int capacity;
Iphone() {}
Iphone(String model, String color, int capacity){
this.model=model;
this.color=color;
this.capacity=capacity;
}
void info() {
System.out.println("model :"+model);
System.out.println("color :"+color);
System.out.println("capacity :"+capacity);
}
}
# 24강 상속과 오버라이딩
- 상속 - 부모클래스의 기능을 자식클래스가 물려받는 것, 기능 재사용 위해
// 다중상속 지원X, 클래스 앞 final 키드는 다른 클래스가 상속 불가능하다.
- 오버라이딩 - 자식클래스에서 부모클래스로부터 받아온 메서드를 재정의 하는 것
- 자식에 맞는 기능으로 맞춰 동작하기 위해 사용
// 오버로딩 : 매서드 중복정의
// 오버라이딩 :매서드 재정의, 상속관계에 있는 자식클래스가 부모클래스의 메서드를 다시 정의하는 것을 의미한다.
//부모 클래스 필드 super 자식 this
- 상속과 생성자 -> super() 부모클래스의 생성자 호출
- 오브젝트 클래스 - 모든 클래스의 조상 클래스를 의미한다(상위 클래스)
public - 전부
private - 자식이 상속받을 수 없는 필드
default - 같은 패키지 안에서만
protected - 다른 패키지에서는 자식클래스만 접근가능
package day24;
public class Day24_1 {
public static void main(String[] args) {
Student st = new Student();
st.breath();
st.learn();
Teacher t = new Teacher();
t.eat();
t.teach();
}
}
class Person{
void breath() {
System.out.println("숨쉬기");
}
void eat() {
System.out.println("밥먹기");
}
void say() {
System.out.println("말하기");
}
}
class Student extends Person{
void learn() {
System.out.println("배우기");
}
}
class Teacher extends Person{
void teach() {
System.out.println("가르치기");
}
}
package day24;
public class Day24_2 {
public static void main(String[] args) {
}
}
final class Parent{}// final class는 상속 불가능하다
class Child extends Parent{}
package day24;
public class Day24_3 {
public static void main(String[] args) {
Leader leader = new Leader();
leader.say();
}
}
class Student2{
void learn() {
System.out.println("배우기");
}
void say() {
System.out.println("말하기");
}
}
class Leader extends Student2{
void lead() {}
void say() {
System.out.println("선생님꼐 인사!");
super.say();
}
}
package day24;
public class Day24_4 {
public static void main(String[] args) {
SportsCar mycar = new SportsCar("red",300);
System.out.println(mycar.color);
System.out.println(mycar.speedLimit);
}
}
class Car{
int wheel;
int speed;
String color;
Car(){}
Car(String color){
this.color=color;
}
}
class SportsCar extends Car{
int speedLimit;
SportsCar(String color,int speedLimit){
this.color=color;
this.speedLimit= speedLimit;
}
}
package day24;
public class Day24_5 {
public static void main(String[] args) {
Aclass a1 =new Aclass();
Aclass a2 =new Aclass();
//toString() : 객체정보를 문자열 출력
System.out.println(a1.toString());
System.out.println(a1);
//equals() : 두 객체가 동일한가를 비교
System.out.println(a1.equals(a2));
//getClass(): 객체의 클래스 정보를 가져옴
System.out.println("a1.getClass()");
}
}
class Aclass{
}
반응형
'Java > Java 인강' 카테고리의 다른 글
Java 인강 필기 8 (0) | 2022.10.02 |
---|---|
Java 인강 필기 7 (0) | 2022.10.02 |
Java 인강 필기 5 (0) | 2022.10.02 |
Java 인강 필기 4 (0) | 2022.10.02 |
Java 인강 필기 3 (0) | 2022.10.02 |