자바 학습 - t6 - 대상 지향

16213 단어

 
 
t6.2
6.2.1 문자열 의 = = 판단:
		// s1         "  Java"
		String s1 = "  Java";
		String s2 = "  ";
		String s3 = "Java";
		// s4                  
		// s4         "  Java"
		String s4 = "  " + "Java";
		// s5                  
		// s5         "  Java"
		String s5 = " " + " " + "Java";
		// s6                  ,
		//             
		String s6 = s2 + s3;
		//   new             String  ,
		// s7          String  
		String s7 = new String("  Java");
		System.out.println(s1 == s4); //   true
		System.out.println(s1 == s5); //   true
		System.out.println(s1 == s6); //   false
		System.out.println(s1 == s7); //   false

6.2.2 두 대상 이 똑 같이 판단 한다.
package com.lee.test.ligang.unit6.t2;

class Person {
	private String name;
	private String idStr;

	public Person() {
	}

	public Person(String name, String idStr) {
		this.name = name;
		this.idStr = idStr;
	}

	//     name idStr setter getter  。
	// name setter getter  
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	// idStr setter getter  
	public void setIdStr(String idStr) {
		this.idStr = idStr;
	}

	public String getIdStr() {
		return this.idStr;
	}

	//   equals()  ,          
	public boolean equals(Object obj) {
		//             
		if (this == obj)
			return true;
		//    obj Person  
		if (obj != null && obj.getClass() == Person.class) {
			Person personObj = (Person) obj;
			//        idStr obj   idStr            
			if (this.getIdStr().equals(personObj.getIdStr())) {
				return true;
			}
		}
		return false;
	}
}

public class OverrideEqualsRight {
	public static void main(String[] args) {
		Person p1 = new Person("   ", "12343433433");
		Person p2 = new Person("   ", "12343433433");
		Person p3 = new Person("   ", "99933433");
		// p1 p2 idStr  ,    true
		System.out.println("p1 p2    ?" + p1.equals(p2));
		// p2 p3 idStr   ,    false
		System.out.println("p2 p3    ?" + p2.equals(p3));
	}
}

6.2.3 eqauls 판단
		int it = 65;
		float fl = 65.0f;
		//    true
		System.out.println("65 65.0f    ?" + (it == fl));
		char ch = 'A';
		//    true
		System.out.println("65 'A'    ?" + (it == ch));
		String str1 = new String("hello");
		String str2 = new String("hello");
		//    false
		System.out.println("str1 str2    ?" + (str1 == str2));
		//    true
		System.out.println("str1  equals str2?" + (str1.equals(str2)));
		//   java.lang.String EqualTest       ,
		//             
		// System.out.println("hello" == new EqualTest());

t6.3 클래스 멤버
t6.4final 수정자
final 구성원 변수
【 규칙 】 【 6.4.1 】 final 수식 의 구성원 변 수 는 할당 되 지 않 음 을 설명 할 수 있 지만 뒤에서 빠 르 고 정적 코드 블록, 구조 방법 에 표 시 된 할당 을 초기 화 해 야 합 니 다.
 
【 규칙 】 매크로 변수의 사용
import java.util.Arrays;

class Person {
	private int age;

	public Person() {
	}

	//        
	public Person(int age) {
		this.age = age;
	}

	//   age setter getter  
	// age setter getter  
	public void setAge(int age) {
		this.age = age;
	}

	public int getAge() {
		return this.age;
	}
}

public class FinalReferenceTest {
	public static void main(String[] args) {
		// final      ,iArr       
		final int[] iArr = { 5, 6, 12, 9 };
		System.out.println(Arrays.toString(iArr));
		//          ,  
		Arrays.sort(iArr);
		System.out.println(Arrays.toString(iArr));
		//        ,  
		iArr[2] = -8;
		System.out.println(Arrays.toString(iArr));
		//      iArr    ,  
		// iArr = null;
		// final  Person  ,p       
		final Person p = new Person(45);
		//   Person   age    ,  
		p.setAge(23);
		System.out.println(p.getAge());
		//      p    ,  
		// p = null;
	}
}
		String s1 = "  Java";
		// s2                  ,
		//   s2           "  Java"   
		String s2 = "  " + "Java";
		System.out.println(s1 == s2);
		//   2       
		String str1 = "  ";     //①
		String str2 = "Java";     //②
		//  str1 str2      
		String s3 = str1 + str2;
		System.out.println(s1 == s3);

[규칙] final 방법의 재 작성
//    :
public class FinalMethodTest {
	public final void test() {
	}
}

class Sub extends FinalMethodTest {
	//              ,    final  
//	public void test() {
//	}
}
//   :

public class PrivateFinalMethodTest
{
	private final void test(){}
}
class Sub extends PrivateFinalMethodTest
{
	//              
	public void test(){}
}

[규칙] 디자인 불가 변류별
class Name {
	private String firstName;
	private String lastName;

	public Name() {
	}

	public Name(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	//   firstName、lastName setter getter  
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return this.firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLastName() {
		return this.lastName;
	}
}

public class Person {
	private final Name name;

	//          final    
	// public Person(Name name) {
	// this.name = name;
	// }

	// public Name getName() {
	// return name;
	// }

	public Person(Name name) {
		this.name = new Name(name.getFirstName(), name.getLastName());
	}

	public Name getName() {
		return new Name(name.getFirstName(), name.getLastName());
	}

	public static void main(String[] args) {
		Name n = new Name("  ", " ");
		Person p = new Person(n);
		// Person   name firstName  "  "
		System.out.println(p.getName().getFirstName());
		//   Person  name firstName 
		n.setFirstName("  ");
		// Person   name firstName    "  "
		System.out.println(p.getName().getFirstName());
	}
}

[모드] 캐 시 인 스 턴 스 의 가 변 클래스
class CacheImmutale
{
	private static int MAX_SIZE = 10;
	//             
	private static CacheImmutale[] cache
		= new CacheImmutale[MAX_SIZE];
	//              ,cache[pos-1]        
	private static int pos = 0;
	private final String name;
	private CacheImmutale(String name)
	{
		this.name = name;
	}
	public String getName()
	{
		return name;
	}
	public static CacheImmutale valueOf(String name)
	{
		//         ,
		for (int i = 0 ; i < MAX_SIZE; i++)
		{
			//         ,          
			if (cache[i] != null
				&& cache[i].getName().equals(name))
			{
				return cache[i];
			}
		}
		//        
		if (pos == MAX_SIZE)
		{
			//            ,                    。
			cache[0] = new CacheImmutale(name);
			//  pos  1
			pos = 1;
		}
		else
		{
			//            ,pos 1
			cache[pos++] = new CacheImmutale(name);
		}
		return cache[pos - 1];

	}
	public boolean equals(Object obj)
	{
		if(this == obj)
		{
			return true;
		}
		if (obj != null && obj.getClass() == CacheImmutale.class)
		{
			CacheImmutale ci = (CacheImmutale)obj;
			return name.equals(ci.getName());
		}
		return false;
	}
	public int hashCode()
	{
		return name.hashCode();
	}
}
public class CacheImmutaleTest
{
	public static void main(String[] args)
	{
		CacheImmutale c1 = CacheImmutale.valueOf("hello");
		CacheImmutale c2 = CacheImmutale.valueOf("hello");
		//        true
		System.out.println(c1 == c2);
	}
}

[규칙] Integer 의 캐 시 정책
		//     Integer  
		Integer in1 = new Integer(6);
		//     Integer  ,      
		Integer in2 = Integer.valueOf(6);
		//         Ineger  
		Integer in3 = Integer.valueOf(6);
		System.out.println(in1 == in2); //   false
		System.out.println(in2 == in3); //   true
		//   Integer   -128~127    ,
		//   200   Integer       。
		Integer in4 = Integer.valueOf(200);
		Integer in5 = Integer.valueOf(200);
		System.out.println(in4 == in5); //  false

내부 클래스
t 6.7.1 비정 상 내부 클래스
package com.lee.test.ligang.unit6.t7.t1;

public class Cow {
	private double weight;

	//             
	public Cow() {
	}

	public Cow(double weight) {
		this.weight = weight;
	}

	//           
	private class CowLeg {
		//              
		private double length;
		private String color;

		//                
		public CowLeg() {
		}

		public CowLeg(double length, String color) {
			this.length = length;
			this.color = color;
		}

		//     length、color setter getter  
		public void setLength(double length) {
			this.length = length;
		}

		public double getLength() {
			return this.length;
		}

		public void setColor(String color) {
			this.color = color;
		}

		public String getColor() {
			return this.color;
		}

		//            
		public void info() {
			System.out.println("       :" + color + ",  :" + length);
			//         private       
			System.out.println("        :" + weight); // ①
		}
	}

	public void test() {
		CowLeg cl = new CowLeg(1.12, "    ");
		cl.info();
	}

	public static void main(String[] args) {
		Cow cow = new Cow(378.9);
		cow.test();
	}
}

package com.lee.test.ligang.unit6.t7.t1;

public class DiscernVariable {
	private String prop = "        ";

	private class InClass {
		private String prop = "        ";

		public void info() {
			String prop = "    ";
			//         .this.varName          
			System.out.println("         :" + DiscernVariable.this.prop);
			//    this.varName           
			System.out.println("         :" + this.prop);
			//         
			System.out.println("      :" + prop);
		}
	}

	public void test() {
		InClass in = new InClass();
		in.info();
	}

	public static void main(String[] args) {
		new DiscernVariable().test();
	}
}

package com.lee.test.ligang.unit6.t7.t1;

public class Outer {
	private int outProp = 9;

	class Inner {
		private int inProp = 5;

		public void acessOuterProp() {
			//                 private    
			System.out.println("    outProp :" + outProp);
		}
	}

	public void accessInnerProp() {
		//                     ,
		//           
		// System.out.println("    inProp :" + inProp);
		//             ,           
		System.out.println("    inProp :" + new Inner().inProp);
	}

	public static void main(String[] args) {
		//       ,         ,         
		Outer out = new Outer(); // ①
		out.accessInnerProp();
	}
}

t. 6.7.2 정적 내부 클래스
package com.lee.test.ligang.unit6.t7.t2;

public class StaticInnerClassTest {
	private int prop1 = 5;
	private static int prop2 = 9;

	static class StaticInnerClass {
		//               
		private static int age;

		public void accessOuterProp() {
			//         :
			//                  
//			System.out.println(prop1);
			//       
			System.out.println(prop2);
		}
	}
}

package com.lee.test.ligang.unit6.t7.t2;
public class AccessStaticInnerClass
{
	static class StaticInnerClass
	{
		private static int prop1 = 5;
		private int prop2 = 9;
	}
	public void accessInnerProp()
	{
		// System.out.println(prop1);
		//         ,       :
		//                
		System.out.println(StaticInnerClass.prop1);
		// System.out.println(prop2);
		//         ,       :
		//                 
		System.out.println(new StaticInnerClass().prop2);
	}
}


내부 클래스 사용 규칙
package com.lee.test.ligang.unit6.t7.t3;

class Out {
	//        ,        ,
	//                   
	class In {
		public In(String msg) {
			System.out.println(msg);
		}
	}
}

public class CreateInnerInstance {
	public static void main(String[] args) {
		Out.In in = new Out().new In("    ");
		/*
		 *              :   OutterClass.InnerClass           Out.In in;
		 *        ,                 Out out = new Out();
		 *         new                    in = out.new In("    ");
		 */
	}
}

package com.lee.test.ligang.unit6.t7.t3;

class StaticOut {
	//          ,        ,
	//                 
	static class StaticIn {
		public StaticIn() {
			System.out.println("         ");
		}
	}
}

public class CreateStaticInnerInstance {
	public static void main(String[] args) {
		StaticOut.StaticIn in = new StaticOut.StaticIn();
		/*
		 *              :   OutterClass.InnerClass           StaticOut.StaticIn
		 * in;   new                   in = new StaticOut.StaticIn();
		 */
	}
}


package com.lee.test.ligang.unit6.t7.t3;

public class SubClass extends Out.In {
	//     SubClass    
	public SubClass(Out out) {
		//      Out      In    
		out.super("hello");
	}
}

t 6.8 Lambda 표현 식
Lambda 입문
package com.lee.test.ligang.unit6.t8.t1;
public interface Command
{
	//       process()      “    ”
	void process(int[] target);
}

package com.lee.test.ligang.unit6.t8.t1;

public class CommandTest {
	public static void main(String[] args) {
		ProcessArray pa = new ProcessArray();
		int[] array = { 3, -4, 6, 4 };
		//     ,              
		pa.process(array, new Command() {
			public void process(int[] target) {
				int sum = 0;
				for (int tmp : target) {
					sum += tmp;
				}
				System.out.println("        :" + sum);
			}
		});
	}
}

package com.lee.test.ligang.unit6.t8.t1;
public class CommandTest2
{
	public static void main(String[] args)
	{
		ProcessArray pa = new ProcessArray();
		int[] array = {3, -4, 6, 4};
		//     ,              ,      
//		pa.process(array , (int[] target)->{
		pa.process(array , target->{
				int sum = 0;
				for (int tmp : target )
				{
					sum += tmp;
				}
				System.out.println("        :" + sum);
			});
	}
}

package com.lee.test.ligang.unit6.t8.t1;

interface Eatable {
	void taste();
}

interface Flyable {
	void fly(String weather);
	default void fly2(String weather){
		System.out.println("     :" + weather);
		System.out.println("       ");
	}
}

interface Addable {
	int add(int a, int b);
}

public class LambdaQs {
	//        Eatable  
	public void eat(Eatable e) {
		System.out.println(e);
		e.taste();
	}

	//        Flyable  
	public void drive(Flyable f) {
		System.out.println("     :" + f);
		f.fly("【       】");
	}

	//        Addable  
	public void test(Addable add) {
		System.out.println("5 3   :" + add.add(5, 3));
	}

	public static void main(String[] args) {
		LambdaQs lq = new LambdaQs();
		// Lambda             ,       。
		lq.eat(() -> System.out.println("       !"));
		// Lambda              ,     【                】
		lq.drive(weather -> {
			System.out.println("     :" + weather);
			System.out.println("       ");
		});
		new Flyable() {
			@Override
			public void fly(String weather) {
				
			}
		}.fly2("【       】");;
		// Lambda             ,     
		//           ,           ,     return   。
		lq.test((a, b) -> a + b);
	}
}

package com.lee.test.ligang.unit6.t8.t1;
public class ProcessArray
{
	public void process(int[] target , Command cmd)
	{
		cmd.process(target);
	}
}

t 6.8.2 Lambda 표현 식 과 인터페이스
java. util. function 대량 함수 식 인터페이스:
XxxFunction:
   return apply();
XxxConsumer:
   void accept();
XxxPredicate:
   Boolean test();
XxxSupplier:
    return getAsXxx();
 
 
 
package com.lee.test.ligang.unit6.t8.t2;

@FunctionalInterface
interface FkTest {
	void run();
}

public class LambdaTest {
	public static void main(String[] args) {
		// Runnable              
		// Lambda             Runnable      、      
		//      Lambda        Runnable  
		Runnable r = () -> {
			for (int i = 0; i < 100; i++) {
				System.out.println();
			}
		};
		// //       :       : Object      
		// Object obj = () -> {
		// for(int i = 0 ; i < 100 ; i ++)
		// {
		// System.out.println();
		// }
		// };

		Object obj1 = (Runnable) () -> {
			for (int i = 0; i < 100; i++) {
				System.out.println();
			}
		};

		//    Lambda               ,      :
		// Lambda                             
		Object obj2 = (FkTest) () -> {
			for (int i = 0; i < 100; i++) {
				System.out.println();
			}
		};

	}
}

 
t. 6.8.3 방법 참조 와 구조 기 참조
 
 
 
1. 인터페이스 에서 의 방법
    1.1 실현        명시 적 수식 static
1.2 무 실현

좋은 웹페이지 즐겨찾기