当前位置: 编程技术>移动开发
本页文章导读:
▪解决ViewPager嵌套后子ViewPager不能滑动的步骤 解决ViewPager嵌套后子ViewPager不能滑动的方法
重写父ViewPager,并在父ViewPager重载onInterceptTouchEvent方法,并返回false,具体是: @Override public boolean onInterceptTouchEvent(MotionEvent motionEvent.........
▪ 向下向上转型 向上向下转型
假设有两个类:Person类是父类,Man类继承自Person类。
第一个例子:
Person p1 = new Man(); // 这就叫 upcasting (向上转型)
Man m1 = (Man)p1; // 这就叫 downcasting (向下转型)
第2个例子:
P.........
▪ 内存储器管理法则 内存管理法则
Basic Memory Management Rules基本内存管理原则The memory management model is based on object ownership. Any object may have one or more owners. As long as an object has at least one owner, it continues to exist. If an obj.........
[1]解决ViewPager嵌套后子ViewPager不能滑动的步骤
来源: 互联网 发布时间: 2014-02-18
解决ViewPager嵌套后子ViewPager不能滑动的方法
重写父ViewPager,并在父ViewPager重载onInterceptTouchEvent方法,并返回false,具体是:
@Override
public boolean onInterceptTouchEvent(MotionEvent motionEvent ) {
return false;
}
这样,子ViewPager就能滑动了。 ]
重写父ViewPager,并在父ViewPager重载onInterceptTouchEvent方法,并返回false,具体是:
@Override
public boolean onInterceptTouchEvent(MotionEvent motionEvent ) {
return false;
}
这样,子ViewPager就能滑动了。 ]
[2] 向下向上转型
来源: 互联网 发布时间: 2014-02-18
向上向下转型
假设有两个类:Person类是父类,Man类继承自Person类。
第一个例子:
Person p1 = new Man(); // 这就叫 upcasting (向上转型) Man m1 = (Man)p1; // 这就叫 downcasting (向下转型)
第2个例子:
Person p2 = new Person(); Man m2 = (Man)p2; // 出错,子类引用不能指向父类对象
注意:向上转型,子类对象会遗失跟父类不同的方法。只能保留重写的方法,来动态绑定。
解释&总结:
1.第一个例子中,p1指向一个子类Man对象,Person p1 = new Man(); 子类m1引用当然可以指向子类Man对象。
2.第二个例子中,p2指向一个父类Person对象,Person p2 = new Person();;子类s1引用不能指向父类对象。
3.向上转型,实际上是将父类引用指向子类对象,这是可行的;但是一定注意子类引用不能直接指向父类对象,这会导致编译器无错误,运行时将出错(ClassCastException异常)。这就是麻雀一定是鸟,但鸟不一定是麻雀的道理(麻雀是鸟的子类)。
4.从第一个例子看出,实际使用中,一般地,如果想向下转型,必须先向上转型。如果当前程序段只有Man m1 = (Man)p1,懒得去上面找是否p1是否是向上转型得来的,可以使用instanceof关键字来避免异常。如下:
if(p1 instanceof Man){ Man m1 = (Man)p1 }
在第二个例子中,Man m2 = (Man)p2; 就可以采用这种形式。这样,就一定不会报错了。因为这里先使用instanceof
来判断p1是否是Man的实例化对象。
[3] 内存储器管理法则
来源: 互联网 发布时间: 2014-02-18
内存管理法则
Basic Memory Management Rules
基本内存管理原则
The memory management model is based on object ownership. Any object may have one or more owners. As long as an object has at least one owner, it continues to exist. If an object has no owners, the runtime system destroys it automatically. To make sure it is clear when you own an object and when you do not, Cocoa sets the following policy:
You own any object you create
你会拥有任何自己创建的对象
You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).
使用名为“alloc”、“new”、“copy”或者“mutableCopy”(比如,alloc,newObject,or mutableCopy)创建一个对象
You can take ownership of an object using retain
你会接管你对它使用retain的对象
A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. You use retain in two situations: (1) In the implementation of an accessor method or an init method, to take ownership of an object you want to store as a property value; and (2) To prevent an object from being invalidated as a side-effect of some other operation (as explained in “Avoid Causing Deallocation of Objects You’re Using”).
When you no longer need it, you must relinquish ownership of an object you own
当你不需要这个对象的时候,你必须放弃这段从属关系
You relinquish ownership of an object by sending it a release message or an autorelease message. In Cocoa terminology, relinquishing ownership of an object is therefore typically referred to as “releasing” an object.
使用release或者autorelease放弃关系
You must not relinquish ownership of an object you do not own
千万不要放弃与自己无关的从属关系
This is just corollary of the previous policy rules, stated explicitly.
Ownership Policy Is Implemented Using Retain Counts
The ownership policy is implemented through reference counting—typically called “retain count” after the retain method. Each object has a retain count.
When you create an object, it has a retain count of 1.
创建对象的时候,retain count为1
When you send an object a retain message, its retain count is incremented by 1.
retain一个对象的时候,该对象的retain count 加1
When you send an object a release message, its retain count is decremented by 1.
release一个对象的时候,该对象的retain count减1
When you send an object a autorelease message, its retain count is decremented by 1 at the end of the current autorelease pool block.
autorelease一个对象的时候,该对象的retain count从当前autorelease pool block的末尾减1
If an object’s retain count is reduced to zero, it is deallocated.
当一个对象的retain count减至零,它会被dealloc
Important: There should be no reason to explicitly ask an object what its retain count is (see retainCount). The result is often misleading, as you may be unaware of what framework objects have retained an object in which you are interested. In debugging memory management issues, you should be concerned only with ensuring that your code adheres to the ownership rules.
Use Accessor Methods to Make Memory Management Easier
Use Accessor Methods to Set Property Values
Suppose you want to implement a method to reset the counter. You have a couple of choices. The first implementation creates the NSNumber instance with alloc, so you balance that with a release.
Don’t Use Accessor Methods in Initializer Methods and dealloc
The only places you shouldn’t use accessor methods to set an instance variable are in initializer methods and dealloc. To initialize a counter object with a number object representing zero, you might implement an init method
Avoid Causing Deallocation of Objects You’re Using
1. When an object is removed from one of the fundamental collection classes.
When an object is removed from one of the fundamental collection classes, it is sent a release (rather than autorelease) message. If the collection was the only owner of the removed object, the removed object (heisenObject in the example ) is then immediately deallocated.
2. When a “parent object” is deallocated.
In some situations you retrieve an object from another object, and then directly or indirectly release the parent object. If releasing the parent causes it to be deallocated, and the parent was the only owner of the child, then the child (heisenObject in the example) will be deallocated at the same time (assuming that it is sent a release rather than an autorelease message in the parent’s dealloc method).
算了。。。写了这么多白写了。。。看官方的内存管理指南就可以了。。多看才有用。。
Basic Memory Management Rules
基本内存管理原则
The memory management model is based on object ownership. Any object may have one or more owners. As long as an object has at least one owner, it continues to exist. If an object has no owners, the runtime system destroys it automatically. To make sure it is clear when you own an object and when you do not, Cocoa sets the following policy:
You own any object you create
你会拥有任何自己创建的对象
You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).
使用名为“alloc”、“new”、“copy”或者“mutableCopy”(比如,alloc,newObject,or mutableCopy)创建一个对象
You can take ownership of an object using retain
你会接管你对它使用retain的对象
A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. You use retain in two situations: (1) In the implementation of an accessor method or an init method, to take ownership of an object you want to store as a property value; and (2) To prevent an object from being invalidated as a side-effect of some other operation (as explained in “Avoid Causing Deallocation of Objects You’re Using”).
When you no longer need it, you must relinquish ownership of an object you own
当你不需要这个对象的时候,你必须放弃这段从属关系
You relinquish ownership of an object by sending it a release message or an autorelease message. In Cocoa terminology, relinquishing ownership of an object is therefore typically referred to as “releasing” an object.
使用release或者autorelease放弃关系
You must not relinquish ownership of an object you do not own
千万不要放弃与自己无关的从属关系
This is just corollary of the previous policy rules, stated explicitly.
Ownership Policy Is Implemented Using Retain Counts
The ownership policy is implemented through reference counting—typically called “retain count” after the retain method. Each object has a retain count.
When you create an object, it has a retain count of 1.
创建对象的时候,retain count为1
When you send an object a retain message, its retain count is incremented by 1.
retain一个对象的时候,该对象的retain count 加1
When you send an object a release message, its retain count is decremented by 1.
release一个对象的时候,该对象的retain count减1
When you send an object a autorelease message, its retain count is decremented by 1 at the end of the current autorelease pool block.
autorelease一个对象的时候,该对象的retain count从当前autorelease pool block的末尾减1
If an object’s retain count is reduced to zero, it is deallocated.
当一个对象的retain count减至零,它会被dealloc
Important: There should be no reason to explicitly ask an object what its retain count is (see retainCount). The result is often misleading, as you may be unaware of what framework objects have retained an object in which you are interested. In debugging memory management issues, you should be concerned only with ensuring that your code adheres to the ownership rules.
Use Accessor Methods to Make Memory Management Easier
Use Accessor Methods to Set Property Values
Suppose you want to implement a method to reset the counter. You have a couple of choices. The first implementation creates the NSNumber instance with alloc, so you balance that with a release.
Don’t Use Accessor Methods in Initializer Methods and dealloc
The only places you shouldn’t use accessor methods to set an instance variable are in initializer methods and dealloc. To initialize a counter object with a number object representing zero, you might implement an init method
Avoid Causing Deallocation of Objects You’re Using
1. When an object is removed from one of the fundamental collection classes.
When an object is removed from one of the fundamental collection classes, it is sent a release (rather than autorelease) message. If the collection was the only owner of the removed object, the removed object (heisenObject in the example ) is then immediately deallocated.
2. When a “parent object” is deallocated.
In some situations you retrieve an object from another object, and then directly or indirectly release the parent object. If releasing the parent causes it to be deallocated, and the parent was the only owner of the child, then the child (heisenObject in the example) will be deallocated at the same time (assuming that it is sent a release rather than an autorelease message in the parent’s dealloc method).
算了。。。写了这么多白写了。。。看官方的内存管理指南就可以了。。多看才有用。。
最新技术文章: