Sometimes it may be appropriate to write a generic method, however it will not be possible for it to accept every type while still maintaining all the necessary functionality.

 

To solve this, use bounded type parameters to restrict generic methods from accepting arguments of particular kind.

 

* accepting 수락 , particular 특정한 restrict  얽매다

 

public <T extends Shape>
	void drawAll(List<T> shapes){
    	for (Shape s : shapes){
        	a.draw(this);
        }
    }
}

 

The above method is used to draw a list of shapes. Writing a generic method with an unbounded type parameter would cause problems because lists of other types cannot be drawn in this way.

 

By specifying that <T extends Shape> we guarantee that only Shape objects can be passed to the method.

 

* The above 위 의 specifying 지정, guarantee 보장하다

'Java - English ver' 카테고리의 다른 글

Type inference in generic calsses  (0) 2024.11.11
Writing generic classes  (0) 2024.11.09
Type Inference in Generic Methods  (3) 2024.11.07
Writing generic methods  (0) 2024.11.06
Writing genenric methods  (0) 2024.11.05

+ Recent posts