# 自訂泛型--限制 ###### tags: 'Ming' {%hackmd BJrTq20hE %} //透過語法限制"price"是一個數字類型的資料 class Book<T>{ T price; public static void show(Book<? extends Number> b){ System.out.println("書籍定價為:" + b.price); } } //寫好的方法 "show" 透過java 會檢查"book" // 即Book<Integer>是否符合extends Number的規則 public class GenericAdv { public static void main(String[] args){ Book<Integer> book = new Book<Integer>(); book.price = 580; Book.show(book); } }
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up