JAVA

· JAVA
/** * 생성자, this, this() 활용 */ public class TestStudent { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("jungyo", false, 100); s1.printStudent(); s2.printStudent(); } // end of main } // end of class class Student { String name = ""; boolean sex; int point; public Student() { name = ""; sex = false; point = 50; } public Student(String name, boolean ..
· JAVA
/** * 생성자, this, this() 활용 */ public class TestBook { public static void main(String[] args) { Book b1 = new Book(); Book b2 = new Book("삼국지","삼국출판", 30000); b1.printBook(); b2.printBook(); } // end of main } // end of class class Book { String name = ""; String publisher = ""; int price; public Book() { this("홍길동전","길동출판", 500); } public Book(String name, String publisher, int price) { this.nam..
· JAVA
/** * 생성자, this, this() 활용 */ public class TestTime { public static void main(String[] args) { Time t1 = new Time(); //t1.hour = 3; t1.minute = 6; t1.second = 23; Time t2 = new Time(10,20,30); t1.printTime(); t2.printTime(); } // end of main } // end of class class Time{ int hour; int minute; int second; public Time() { this(11,22,33); } public Time(int hour, int minute, int second) { this.hour ..
· JAVA
/** * 생성자 Constructor, this, this() */ public class TestCar { public static void main(String[] args) { Car c1 = new Car("아반떼", "은색", 130); Car c2 = new Car("티코", "빨강색", 100); c1.printCar(); c1.speedDown(); c1.speedDown(); c1.speedDown(); c1.printCar(); c1.stopCar(); c1.printCar(); Car c3 = new Car(); c3.printCar(); } // end of main } // end of class class Car { String name = ""; // 전역변수 String c..
· JAVA
/** * 생성자 Constructor : 모든 class 에는 반드시 하나 이상의 생성자가 있어야한다 * class 에 생성자를 하나도 만들지 않으면, 컴파일러가 기본생성자를 추가해준다 */ public class TestMart { public static void main(String[] args) { Mart m1 = new Mart(); m1.name = "2마트"; m1.ball = 10000; m1.pen = 1000; Mart m2 = new Mart("홈마이너스", 11000,1100); //m2.name = "홈마이너스"; //m2.ball = 11000; //m2.pen = 1100; m1.printPrice(); m2.printPrice(); System.out.println("세일..
· JAVA
/** * 클래스 활용 */ public class TestCard { public static void main(String[] args) { Card c1 = new Card(); c1.kind = "하트"; c1.number=7; Card c2 = new Card(); c2.kind = "다이아몬드"; c2.number=1; c1.printCard(); c2.printCard(); } // end of main } // end of class class Card { String kind = ""; int number; void printCard() { System.out.println(kind+number); } }
· JAVA
/** * 객체지향 : 기존 절차지향언어 + 클래스class 개념 * 클래스class : 새로운 타입의 정의, = 구조체 + 메서드 * 변수의 타입 : 기본형(8가지), 참조형(기본형8가지 아닌것) */ public class TestMyClass { public static void main(String[] args) { char b; // 변수 선언 (기본형) b = 'w'; // 변수 초기화 char c = 'w'; // 변수 선언, 초기화 AAA a; // 참조변수 선언 (참조형) a = new AAA(); // 참조변수의 객체 생성 (메모리할당) AAA a2 = new AAA(); // 참조변수 선언, 객체생성 a.w = 100; // 참조변수 값의 초기화 a.t = true; // . 돗트 연..
· JAVA
/** * 클래스 작성 */ // public 접근제어자는 파일명과 같은 클래스 앞에만 사용할 수 있다 public class TestBook { int a; // 전역변수 public static void main(String[] args) { int x; // 지역변수 - 메서드 안쪽에서 선언한 변수 //System.out.println(x); // 지역변수 : 초기화 전에 사용하면 에러 Book b1 = new Book(); // 참조변수 선언, 객체 생성을 한번에 b1.name = "나는 자바다"; b1.publisher = "한빛"; b1.price = 30000; //System.out.println(b1.name + b1.publisher + b1.price); b1.printBook(); ..
· JAVA
/** * 메서드 활용 */ public class TestMethod3 { static int sumA(int a) { // 인자값 숫자 + 10 을 리턴 return a + 10; } static long sum(int a, int b) { return (long)a + b; } static long sub(int a, int b) { return (long)a - b; } static long mul(int a, int b) { return (long)a * b; } static double div(int a, int b) { return (double)a / b; // 소수점 손실 } static int mod(int a, int b) { return a % b; } static int abs(i..
· JAVA
import java.util.Scanner; /** * 메서드 활용 */ public class TestMethod2 { static void printHour() { System.out.println("현재시간 12시"); } static int yourAge() { return 26; } static void printName(String name) { System.out.println(name); } static String getName() { Scanner scan = new Scanner(System.in); System.out.println("이름을 입력하세요"); String name = scan.nextLine(); return name; } public static void main(..
'JAVA' 카테고리의 글 목록 (5 Page)