Java Notes-4

-Note that the jar command automatically adds a directory called META-INF to our

archive.

-Aside from attributes, you can put a few special values in the manifest file. One of these,
Main-Class , allows you to specify the class containing the primary  main() method for
an application contained in the JAR

Main-Class: com.oreilly.Game

-To permit our  EvilEmpire example to make a network connection, we need to create a
policy file that contains the appropriate permission. 

-The policy file you just created is not complicated. Take a look at it with a text editor,
which shows the simple syntax of the policy we created:

-The  hashCode() method returns an integer that is a hashcode for the object. 

- reflection is the ability for a class
or object to examine itself. Reflection lets Java code look at an object (more precisely,
the class of the object) and determine its structure

getFields() returns an array of  Field objects representing all a class’s public vari‐
ables, including those it inherits.
• getDeclaredFields() returns an array representing all the variables declared in
the class, regardless of their access modifiers, but not including inherited variables.
• For constructors, the distinction between “all constructors” and “declared con‐
structors” is not meaningful (classes do not inherit constructors), so  getConstruc
tors() and  getDeclaredConstructors() differ only in that the former returns
public constructors while the latter returns all the class’s constructors.

-All of the types of members of a Java class—fields, methods, constructors, and inner
classes—have a method  getModifiers() that returns a set of flags indicating whether
the member is private, protected, default level, or publicly accessible. 

-The class  java.lang.reflect.Field represents static variables and instance variables.
Field has a full set of overloaded accessor methods for all the base types (for example,
getInt() and  setInt() ,  getBoolean() and  setBoolean() ), and  get() and  set()
methods for accessing fields that are reference types.

-Subject
to the normal security rules, a  Method object’s  invoke() method can be used to call the
underlying object’s method with specified arguments. Yes, Java does have something
like a method pointer!


-We can run  invoke to fetch the value of the system clock:

-The  java.lang.reflect.Constructor class represents an object constructor in the
same way that the  Method class represents a method. You can use it, subject to the security
manager, of course, to create a new instance of an object, even with constructors that
require arguments. 


-The Reflection API allows you to create and inspect arrays of base types using the
java.lang.reflect.Array class

-With the addition of generics, types are no longer simply one-to-one with Java classes
and interfaces but can be parameterized on one or more types to create a new, generic
type.

-annotations, a feature that allows metadata to be added
to Java classes, methods, and fields. 

-The  Proxy class is a factory that can generate an adapter class, implementing
any interface (or interfaces) you want. When methods are invoked on the adapter class,
they are delegated to a single method in a designated  InvocationHandler object. You
can use this to create dynamic implementations of any kind of interface at runtime and
handle the method calls anywhere you want. 

-More appropriately, you can use reflection in situations where you need to work with
objects that you can’t know about in advance. 

-Annotations allow you to add metadata to Java packages, classes, methods, and fields.
This metadata can be utilized by tools at compile time and optionally retained in the
compiled Java classes for use at runtime as well. 


-Generics are about abstraction. Generics let you create classes and methods that work
in the same way on different types of objects. The term generic comes from the idea that
we’d like to be able to write general algorithms that can be broadly reused for many
types of objects rather than having to adapt our code to fit each circumstance. 

-Any child of a type of object can serve
in place of its parent type and, ultimately, every object is a child of  java.lang.Object ,
the object-oriented “Eve,” so to speak.

-Generics are an enhancement to the syntax of classes that allow us to specialize the class
for a given type or set of types. 

-t is still possible to use raw types in Java just as before generics were added to the
language. The only difference is that the Java compiler generates a warning wherever
they are used in an “unsafe” way. For example:


-Remember that all type information is erased in the compiled class. The raw type does
not have any way of knowing the type of object you want to construct at runtime. 

T element = new T(); // Error! Invalid syntax.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值