1 / 43

클래스 이해하기

클래스 이해하기. 클래스 작성 방법. 클래스 작성 class class_name { public int Varname; // 멤버변수 public voint Print(){ } // 메소드 } class 선언하기 class_name name = new class_name(); 멤버 변수 , 메소드 사용하기 name.Varname=10 name.Print();. // 클래스 작성 방법 class MemberVar{

nydia
Download Presentation

클래스 이해하기

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 클래스 이해하기

  2. 클래스 작성 방법 • 클래스 작성 class class_name{ public int Varname; //멤버변수 public voint Print(){ } //메소드 } • class 선언하기 • class_namename = newclass_name(); • 멤버 변수, 메소드 사용하기 • name.Varname=10 • name.Print(); 컴퓨터과학과 비젼랩

  3. //클래스 작성 방법 class MemberVar{ public static void main(String args[]){ data1objectVar = new data1(); objectVar.intVar = 10; objectVar.strVar = “kim”; System.out.println(objectVar.intVar); } } class data1{ int intVar; String strVar; } 컴퓨터과학과 비젼랩

  4. 객체 생성 • data1objectVar = new data1(); class data1 objectVar int intVar; String strVar; int intVar; String strVar; new 연산자는 객체를 복사 컴퓨터과학과 비젼랩

  5. 객체 생성자란? • 생성자 • 클래스 이름이 같은 함수 • data1 objectVar = new data1(); • 역할 • 멤버 변수의 초기화, 객체 생성 • 생성자가 없는 클래스는 내부적으로 생성 class data1{ int intVar; String strVar; data1(){ }// 내부적으로 생성 } 컴퓨터과학과 비젼랩

  6. //생성자 함수를 사용하지 않는 형태 class Salary{ String name; double amount; } class Payroll{ public static void main(String args[]){ Salary sal = new Salary(); sal.name=“kim young soo”; sal.amount=1000000.0; System.out.print(sal.name+“:”+sal.amount); } }

  7. //생성자 class Salary{ String name; double amount; Salary() { } } class Payroll{ public static void main(String args[]){ Salary sal = new Salary(); sal.name=“kim young soo”; sal.amount=1000000.0; System.out.print(sal.name+“:”+sal.amount); } }

  8. //생성자 함수 활용 class Salary{ String name; double amount; Salary(String n, double a) { name = n; amount = a; } } class Payroll{ public static void main(String args[]){ Salary sal = new Salary(“park js”,100000.0); System.out.print(sal.name+“:”+sal.amount); } }

  9. //함수 or 생성자 구분 class Salary{ String name; double amount; voidSalary(String n, double a) { name = n; amount = a; } } class Payroll{ public static void main(String args[]){ Salary sal = new Salary(“kim yw”,100000.0); System.out.print(sal.name+“:”+sal.amount); } }

  10. 생성자 오버로딩

  11. class Salary{ //생성자 오버로딩 String name; double amount; Salary(){ } Salary(String n, double a) { name = n; amount = a; } void setName(String name){ this.name = name; } } class Payroll{ public static void main(String args[]){ Salary salOne = new Salary(); salOne.setName(“kim young won”); System.out.print(salOne.name+“:”+salOne.amount); } }

  12. 메소드 오버로딩

  13. class OverloadDemo{ void test(){ System.out.println(“No parameters”); } void test(int a, int b){ System.out.println(“a and b:”+a+ “ ”+b);} double test(double a){ System.out.println(“double a:”+a); return a*a; } } class Overload{ public static void main(String[] args){ OverloadDemo ob = new OverloadDemo(); double result; ob.test(); ob.test(10); ob.test(10,20); result = ob.test(123.2); System.out.println(“Result of ob.test(123.2):”+result); } }

  14. class Salary{ //메소드 오버로딩 String name; double amount; Salary(String n, double a) { name = n; amount = a; } void raiseSalary(double byPercent){amount*=1+byPercent/100;} void raiseSalary(double byPercent, double bonus){ amount+= amount*byPercent/100+amount*bonus/100; } } class Payroll{ public static void main(String args[]){ Salary salOne = new Salary(“ywkim”,1000000.0); salOne.raiseSalary(5,4); System.out.print(salOne.name+“:”+salOne.amount); } }

  15. 참조값 바꾸기 • call-by-value • call-by-reference 컴퓨터과학과 비젼랩

  16. //call-by-value class CallTest{ publicstaticvoid main(String[] argv){ int num1=10, num2=20; System.out.println("num="+num1+ "num2="+num2); new CallTest().swap(num1,num2); System.out.println("num="+num1+ "num2="+num2); } void swap(int a,int b){ int temp; temp = a; a = b; b = temp; } }

  17. class CallTest{ publicstaticvoid main(String[] argv){ Int num1 = new Int(10), num2 = new Int(20); System.out.println("num="+num1+ "num2="+num2); new CallTest().swap(num1,num2); System.out.println("num="+num1+ "num2="+num2); } void swap(Int a,Int b){ Int temp; temp = a; a = b; b = temp; System.out.println(a.vlaue + “ “ + b.value); } } class Int{ int value; Int(int value) { this.value = value; } public String toString(){ return Integer.toString(value); } }

  18. class CallTest{// call-by-reference publicstaticvoid main(String[] argv){ Int num1 = new Int(10), num2 = new Int(20); System.out.println("num="+num1+ "num2="+num2); new CallTest().swap(num1,num2); System.out.println("num="+num1+ "num2="+num2); } void swap(Int a,Int b){ int temp; temp = a.value; a.value = b.value; b.value = temp; } } class Int{ int value; Int(int value) { this.value = value; } public String toString(){ return Integer.toString(value); } }

  19. 멤버 변수의 상속

  20. class SchoolMember{ private String name = “kim young won"; } class Student extends SchoolMember{ public long studentnumber = 200100001; } class StudentManager{ publicstaticvoid main(String args[]){ Student st = new Student(); System.out.println(st.name); System.out.println(st.studentnumber); } }

  21. 상속

  22. class A{ int i; int j; void setij(int x,int y){ i = x; j = y;} } class B extends A{ int total; void sum(){ total = i +j; } } class Access{ publicstaticvoid main(String args[]){ B subOb = new B(); subOb.setij(10,20); subOb.sum(); System.out.println("총합은" + subOb.total); } }

  23. 수퍼(super) 클래스의 private 멤버접근

  24. class SchoolMember{ private String name = “kim y w"; String getName(){ return name;} } class Student extends SchoolMember{ public long studentnumber = 20010001; } class StudentManager{ publicstaticvoid main(String args[]){ Student st = new Student(); System.out.println(st.getName()); } }

  25. 오버라이딩

  26. class SchoolMember{ String name; publicvoid register(String na){ name = na; } } class Student extends SchoolMember{ publicvoid register(String na){ name = na + "Sir"; } } class StuMgn{ publicstaticvoid main(String args[]){ Student stu1 = new Student(); stu1.register("kim"); System.out.println(stu1.name); SchoolMember stu2 = new Student(); stu2.register("kim"); System.out.println(stu2.name); } }

  27. class A{ int i,j; A(int a, int b) { i = a; j = b;} void show(){ System.out.println(“i와 j”+ i+j);} } class B extends A{ int k; B(int a,int b,intc){ super(a,b); k = c; } void show(){ System.out.println(“k는”+k);} } class Ovrride{ public static void main(String args[]){ B subOb = new B(1,2,3); subOb.show(); } }

  28. class A{ int i,j; A(int a, int b) { i = a; j = b;} void show(){ System.out.println(“i와 j”+ i+j);} } class B extends A{ int k; B(int a,int b,int c){ super(a,b); k = c; } void show(String msg){ System.out.println(msg + k);} } class Ovrride{ public static void main(String args[]){ B subOb = new B(1,2,3); subOb.show(“k는”); subOb.show(); } }

  29. 추상 클래스

  30. abstract class Figure{ //추상 클래스 정의 double dim1; double dim2; Figure(double a, double b){dim1 = a; dim2 = b;} abstract double area(); } class Rectangle extends Figure{ Rectangle(double a, double b){ super(a,b);} double area(){ System.out.println(“사각형 내부”); return dim1*dim2; } } class Triangle extends Figure{ Triangle(double a, double b){super(a,b);} double area(){ System.out.println(“삼각형 내부”); return dim1*dim2/2; } }

  31. class AbstractAreas{ // 추상화클래스 public static void main(String args[]){ Rectangle r = new Rectangle(9,5); Triangle t = new Triangle(10,8); Figure figref; figref = r; System.out.println(“사각형 면적은”+figref.area()); figref = t; System.out.println(“삼각형 면적은”+figref.area()); } }

  32. 인터페이스

  33. interface Callback{ //인터페이스 정의 void callback(int param); } class Client implements Callback{ public void callback(int p){ System.out.println(“callback called with”+p); } void nonIfacemethod(){ System.out.println(“Classes that implement interface”+”may also define other mebers, too.”); } } class AnotherClient implements callback{ public void callback(int p){ System.out.println(“Another version of callback”); System.out.println(“p squaed is”+(p*p)); } }

  34. class TestIface{ //인터페이스 호출 public static void main(String args[]){ Callback c = new Client(); AnotherClient ob = new AnotherClient(); c.callback(10); c = ob; c.callback(10); } }

  35. interface A{ //인터페이스 메소드는 모두 재정의 void meth1(); } interface B extends A{ void meth2(); void meth3(); } class Myclass implements A, B{ publci void meth1(){ System.out.println(“Implement meth1());} publci void meth2(){ System.out.println(“Implement meth2());} publci void meth3(){ System.out.println(“Implement meth3());} public static void main(Strin[] args){ Myclass m = Myclass(); m.meth1(); m.meth2(); m.meth3(); } }

  36. 동일 클래스내의 변수 접근& 스테틱(static) 변수 접근

  37. class StaticDemo{ staticint a = 20; staticint b = 30; staticvoid callme(){ System.out.println("a="+a); } } class StaticByName{ publicstaticvoid main(String[] args){ StaticDemo.callme(); System.out.println("b="+StaticDemo.b); } }

  38. 밑줄 친 a변수에는 몇이 들어갈까요? class StaticShare{ static int a=0; void inc(){ a = a +2;} } class TestStatic{ public static void main(String[] args){ StaticShare s1 = new StaticShare(); StaticShare s2 = new StaticShare(); StaticShare s3 = new StaticShare(); s1.inc(); s2.inc(); s3.inc(); System.out.println(StaticShare.a); } } 컴퓨터과학과 비젼랩

  39. Integer Class • 메소드 : static int parseInt(String s) import java.lang.*; publicclass Integertest{ publicstaticvoid main(String[] args){ int i,sum=0; for(i = 0 ; i < args.length ; i++){ sum += Integer.parseInt(args[i]); } System.out.println(sum); } } 컴퓨터과학과 비젼랩

  40. Integer Class • double doubleValue() : Returns the value of this Integer as a double. • float floatValue() : Returns the value of this Integer as a float. • static Integer getInteger(String nm) • int intValue() : Returns the value of this Integer as an int. • long longValue() : Returns the value of this Integer as a long. • static int parseInt(String s) : Parses the string argument as a signed decimal integer. • short shortValue() : Returns the value of this Integer as a short. • String toString() : Returns a String object representing this Integer's value. 컴퓨터과학과 비젼랩

  41. 한 문자씩 읽어오기 import java.io.*; public class ReadUsage{ public static void main(String args[]) throws IOException{ char c; System.out.println(“please input…, ‘q’ to quit.”); do{ c = (char) System.in.read(); System.out.print( c ); }while(c != ‘q’); } } 컴퓨터과학과 비젼랩

  42. InputStream Class • 키보드로 부터 입력 • abstractint read(): Reads the next byte of data from the input stream. • Buffer에 저장된 내용을 입력 • int read(byte[] b): • Reads some number of bytes from the input stream and stores them into the buffer array b. • int read(byte[] b, int off, int len): • Reads up to len bytes of data from the input stream into an array of bytes 컴퓨터과학과 비젼랩

  43. OutputStream Class • 콘솔에 출력 • abstractvoid write(int b): Writes the specified byte to this output stream. • OutputStream class의 인스턴스에 출력 • void write(byte[] b): • Writes b.length bytes from the specified byte array to this output stream. • void write(byte[] b, int off, int len): • Writes len bytes from the specified byte array starting at offset off to this output stream. • void close(): • Closes this output stream and releases any system resources associated with this stream. • Wirte()함수 호출 한 다음 반드시 호출 컴퓨터과학과 비젼랩

More Related