我创建了如下所示的类。但是不调用 Override 方法。我错过了什么吗?否则我需要添加任何关键字(like Virtual
)以用作覆盖?
MyWebView.java
package com.example.stackoverflow;
import android.content.Context;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
public class MyWebView extends WebView
{
WebView localView;
static Context localcontext;
A aclass;
public MyWebView(Context context)
{
super(context);
localcontext=context;
}
public LinearLayout init()
{
LinearLayout layout=new LinearLayout(localcontext);
localView=new WebView(localcontext);
localView.setWebViewClient(new MyClient());
aclass=new A();
layout.addView(localView);
return layout;
}
class MyClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
aclass.startMyMethod();
return super.shouldOverrideUrlLoading(view, url);
}
}
}
爪哇
package com.example.stackoverflow;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.app.Activity;
public class A extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyWebView view=new MyWebView(this);
LinearLayout layout = view.init();
view.loadUrl("https://www.google.co.in/");
setContentView(layout);
}
public void startMyMethod()
{
System.out.println("ParentMethod");
}
}
B.java(MainActivity)
package com.example.stackoverflow;
import android.os.Bundle;
public class B extends A {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void startMyMethod() {
System.out.println("ChildMethod");
super.startMyMethod();
}
}
注意:B 类是客户端类。其他两个在 jar 文件中。我如何在 jar 文件(库源)中使用 new B()。名称 B 可能会更改。如果父方法被覆盖,则不应执行父内容