mCallback = (OnHeadlineSelectedListener) activity;
这是一个Android应用教程的一些java代码。我不明白这是做什么的。我知道它正在为mCallback赋值但是什么?为什么OnHeadlineSelectedListener在括号中然后活动对象就在它后面,那该怎么办?
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.vizoplex.my.first.app;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// The container Activity must implement this interface so the frag can deliver messages
public interface OnHeadlineSelectedListener {
/** Called by HeadlinesFragment when a list item is selected */
public void onArticleSelected(int position);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We need to use a different list item layout for devices older than Honeycomb
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
// Create an array adapter for the list view, using the Ipsum headlines array
setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
}
@Override
public void onStart() {
super.onStart();
// When in two-pane layout, set the listview to highlight the selected list item
// (We do this during onStart because at the point the listview is available.)
if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
mCallback.onArticleSelected(position);
// Set the item as checked to be highlighted when in two-pane layout
getListView().setItemChecked(position, true);
}
}
答案 0 :(得分:2)
它正在尝试将该活动投射到OnHeadlineSelectedListener
,可能是因为此实际活动扩展了Listener
。这样,mCallback充当正确定义回调的侦听器。
修改强>:
你有:
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
它正试图将activity
投射到OnHeadlineSelectedListener
。如果activity
未实现此侦听器,则抛出异常。这条消息很简单:
activity.toString()+ " must implement OnHeadlineSelectedListener"
答案 1 :(得分:2)
那是类型转换。 例如: 你有Animal接口有两种方法,运行和吃。
public interface Animal {
public String run();
public String eat(String food);
}
你有课程,它实现了它,比如,Cat课程。 Cat类有一些额外的方法,划痕和喵喵。
public class Cat implements Animal{
public String run(){
return "Run as a cat";
}
public String eat(){
return "Eat fish";
}
public String mew(){
return "MEW!!!!";
}
public String scratch(){
return "scratch-scratch-scratch";
}
}
另外,你有Dog类,它有树皮方法。
public class Dog implements Animal{
public String run(){
return "Run as a dog";
}
public String eat(){
return "Eat meat";
}
public String bark(){
return "BARK!!!!";
}
}
所以,你经常需要和一些动物(狗或猫)一起使用动物。但编译器cat没有你的帮助就不能进行这种投射。所以,你必须告诉它你到底想要什么:
public void Show{
Cat cat = new Cat(); //you have a cat
Dog dog = new Dog(); //and a dog
Animal animal = null; //and animal
dog = (Dog)animal; //and here is where compiler can't convert animal to dog without your help.
Animal animal2 = cat; //But this casting it can make without your help, as it exactly knows what to do.
}
This one book对学习Java非常有用。如果你真的对理解这些东西感兴趣,你应该阅读它。关于铸造如何工作的完整解释也可以在本书中找到。
答案 2 :(得分:1)
activity
与OnHeadlineSelectedListener
对象casted mCallback
,{{1}}正在分配给该对象。
答案 3 :(得分:1)
我认为mCallback对象是activity对象类的子类。 因此,此语句将活动对象强制转换为更具体的mCallback对象。
有关投射Click here
的更多信息答案 4 :(得分:0)
它正在将活动转换为OnHeadlineSelectedListener。假设该活动实现了OnHeadlineSelectedListener。这样做是为了您可以直接调用OnHeadlineSelectedListener接口中的方法。
答案 5 :(得分:0)
我最近已经转出Java了,但它看起来好像该行是投射 activity
到类型OnHeadlineSelectedListener
。如果类型不兼容,则转换将一种类型转换为另一种类型,并抛出异常(一种或另一种形式)。
语法是:
variable = (Type) variableOfType2;