这是一个面试问题.
子类是否继承私有字段?
我回答"否",因为我们无法使用"正常的OOP方式"访问它们.但是访谈者认为它们是继承的,因为我们可以间接访问这些字段或使用反射,它们仍然存在于对象中.
我回来后,在javadoc中找到以下引用:
超级私人会员
子类不继承其父类的私有成员.
你知道面试官的意见吗?
我知道在Java中,静态方法就像实例方法一样继承,不同之处在于,当重新声明它们时,父实现被隐藏而不是被覆盖.好吧,这很有道理.但是,Java教程指出了这一点
接口中的静态方法永远不会被继承.
为什么?常规和接口静态方法有什么区别?
当我说静态方法可以继承时,让我澄清一下我的意思:
class Animal {
public static void identify() {
System.out.println("This is an animal");
}
}
class Cat extends Animal {}
public static void main(String[] args) {
Animal.identify();
Cat.identify(); // This compiles, even though it is not redefined in Cat.
}
Run Code Online (Sandbox Code Playgroud)
然而,
interface Animal {
public static void identify() {
System.out.println("This is an animal");
}
}
class Cat implements Animal {}
public static void main(String[] args) {
Animal.identify();
Cat.identify(); // This does not compile, because interface …Run Code Online (Sandbox Code Playgroud) 我在JUnit中有很多测试用例.所有这些都需要在@BeforeClass静态方法中执行相同的代码.这是代码重复,我试图摆脱它.这样做的一种肮脏方式是继承.JUnit中是否还有其他机制可能会有所帮助?
PS.我写了这篇关于这个主题的博客文章:http://www.yegor256.com/2015/05/25/unit-test-scaffolding.html
鉴于以下课程:
public abstract class Super {
protected static Object staticVar;
protected static void staticMethod() {
System.out.println( staticVar );
}
}
public class Sub extends Super {
static {
staticVar = new Object();
}
// Declaring a method with the same signature here,
// thus hiding Super.staticMethod(), avoids staticVar being null
/*
public static void staticMethod() {
Super.staticMethod();
}
*/
}
public class UserClass {
public static void main( String[] args ) {
new UserClass().method();
}
void method() {
Sub.staticMethod(); // …Run Code Online (Sandbox Code Playgroud) 关于包含静态抽象Java方法的不可能性存在许多问题.关于此的解决方法(设计缺陷/设计强度)也有很多.但我找不到任何关于我即将陈述的具体问题.
在我看来,制作Java的人和很多使用Java的人并没有像我和其他许多人那样思考静态方法 - 作为类函数或属于类的方法而不是任何对象.那么还有其他一些实现类函数的方法吗?
这是我的例子:在数学中,一个组是一组对象,可以使用某些操作*以某种合理的方式相互组合 - 例如,正实数在正常乘法下形成一组(x*y = x × y),并且整数组形成一组,其中"乘法"运算是加法(m*n = m + n).
在Java中对此进行建模的一种自然方法是为组定义接口(或抽象类):
public interface GroupElement
{
/**
/* Composes with a new group element.
/* @param elementToComposeWith - the new group element to compose with.
/* @return The composition of the two elements.
*/
public GroupElement compose(GroupElement elementToComposeWith)
}
Run Code Online (Sandbox Code Playgroud)
我们可以为上面给出的两个例子实现这个接口:
public class PosReal implements GroupElement
{
private double value;
// getter and setter for this field …Run Code Online (Sandbox Code Playgroud) 我想回答以下问题:
子类不会继承父类的某些成员.说出三个这样的成员.
我知道私有成员不会继承到子类,并且默认成员不会在包外继承.任何人都可以完成答案吗?
编辑: - 我相信静态成员是根据以下演示继承的
public class sup {
public static void main(String agr[]){
}
protected static int staticInt=0;
protected final int finalInt=3;
protected int protectedInt=0;
public String publicString = "";
private int privateInt=8;
}
class sub extends sup{
public void del(){
staticInt=1;
staticInt=finalInt;
}
}
Run Code Online (Sandbox Code Playgroud) class DonkeyBattler {
static void doBattle(){
System.out.println("Weaponized donkey battling");
}
}
class FunkyBattler extends DonkeyBattler {
static void doBattle(){
System.out.println("Weaponized donkey battling with bellbottoms");
}
}
Run Code Online (Sandbox Code Playgroud)
doBattle方法应该是重新定义还是覆盖?哦顺便说一句,这是Java.
我需要通过覆盖它的一些方法来扩展我的主类的功能.我期待扩展主类的类能够运行.但是,Eclipse不会将MyLauncher识别为可运行的类.在下面的代码中,我有一个setup()被子类覆盖的方法.我想要的是一种main(..)从超类运行的方法,还有从子类运行的设置.
// Launcher.java
public class Launcher {
Launcher instance;
public static void main (args[]) {
instance = new Launcher(); // This is likely the problem
instance.setup();
}
public void setup() {
System.out.println("Default agent setup.");
}
}
// MyLauncher.java
public class MyLauncher extends Launcher {
public void setup() {
System.out.println("New agent setup!");
}
}
Run Code Online (Sandbox Code Playgroud)
我接受这个的替代方案.但是我无法在子类中添加main方法.Launcher类在我正在制作的API中,因此它不能引用正在使用API的类MyLauncher.
编辑:我认为这是针对我的问题.我决定寻找新的方法.由于我正在使用JDT,我将解析Launcher并注入该类.
要在没有 Firebase Cloud Messaging 的情况下获取 Unity 中的当前活动,请使用以下代码:
var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
Run Code Online (Sandbox Code Playgroud)
但是,Firebase Cloud Messaging 扩展了默认的 Unity Activity。所以我改变了我的AndroidJavaClass:
var unityPlayer = new AndroidJavaClass("com.google.firebase.MessagingUnityPlayerActivity");
var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
Run Code Online (Sandbox Code Playgroud)
但是,我现在在我的 logcat 中收到以下错误:
08-01 15:51:31.838 20323 20375 E Unity : AndroidJavaException: java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "currentActivity" in class "Lcom/google/firebase/MessagingUnityPlayerActivity;" or its superclasses
08-01 15:51:31.838 20323 20375 E Unity : java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "currentActivity" in class "Lcom/google/firebase/MessagingUnityPlayerActivity;" or its superclasses
08-01 15:51:31.838 20323 20375 E Unity : …Run Code Online (Sandbox Code Playgroud) c# android unity-game-engine android-activity firebase-cloud-messaging
隐藏静态字段时,字段对子类的访问级别没有限制,它甚至可以是非静态的,也可以是其他数据类型.
另一方面,当隐藏静态方法时,从超类隐藏静态方法的子类中的静态方法可以允许比隐藏方法更多但不能更少的访问.
AFAIK,静态方法链接无论如何都是在编译时完成的,那么为什么会有这样的限制呢?
PS问题只是出于好奇.