I want to replace Java code with Kotlin code.

Asked 2 years ago, Updated 2 years ago, 86 views

I tried replacing Java with Kotlin as shown in the code below, but if I remove the comment out, I get a compilation error.

Java is longer, so it may have become a Kotlin code like Java, but what do you think is the cause?

Original Java Code

class Super {
   static String x = "Super:x";
   Stringy = "Super:y";
   static void methodA() {
       System.out.println("Super:methodA()");
   }
   void methodB(){
       System.out.println("SupermodB()
   }
}
class Subextends Super {
   static String x = "Sub:x";
   Stringy = "Sub:y";
   static void methodA() {
       System.out.println("Sub:methodA()");
   }
   void methodB(){
       System.out.println("Sub:methodB()");
   }
}
US>class Sample {
   public static void main(String[]args) {
        Superobj = new Sub();
        System.out.println(obj.x);
        System.out.println(obj.y);
        obj.methodA();
        obj.methodB();
   }
}

Kotlin Replacement Code

openclass Super{
     open value: String = "Super:y"

     US>companion object {
          valx: String = "Super:x"

          fun methodA(){
                println("Super:methodA()"
          }
     }

     open fun methodB(){
           println("Super:methodB()"
     }
}
classSub:Super() {
      override value:String="Sub:y"

      US>companion object {
           valx: String="Sub:x"

           fun methodA(){
                 println("Sub:methodA()"
           }
      }

      override fun methodB(){
            println("Sub:methodB()"
      }
}
funmain(args:Array<String>){
       valobj —Super=Sub()
       // println(obj.x)
       println(obj.y)
       // obj.methodA()
       obj.methodB()
}

java kotlin

2022-09-30 18:14

1 Answers

In the first place, the original Java code is an example of how to write "this code gives you strange results."

  • Declare Hidden Fields

    In Java, a field on the subclass side becomes a different field such as hiding a field on the parent class side.The compiler uses the "Viewing at Compilation" field.

  • staticCalling from an instance of a member

    Java determines which class of static members to use based on the type determined at compile time.

Declare Hidden Fields

In Java, a field on the subclass side becomes a different field such as hiding a field on the parent class side.The compiler uses the "Viewing Side at Compilation" field.

staticcalling from a member instance

Java determines which class of static members to use based on the type determined at compile time.

Both should be alerted by the current Java compiler.Let's not ignore these warnings.

In order to prevent such misuse, Kotlin cannot do the same thing in the first place.

  • Cannot declare field directly

    Kotlin's property declaration is like a Java declaration of a field, getter, and setter.Therefore, you can override as well as the method, and the behavior is similar to that of the method override.

  • static members cannot be defined

    Kotlin's companion is used to declare properties and methods of different objects tied to a particular class. Although it is considered an alternative to static, it does not behave exactly like Java's static and cannot be invoked through an instance.

Cannot declare field directly

Kotlin's property declaration is like a Java declaration of a field, getter, and setter.Therefore, you can override as well as the method, and the behavior is similar to that of the method override.

static members cannot be defined

Kotlin's companion is used to declare "properties and methods of different objects tied to a particular class." Although it is considered an alternative to static, it does not behave exactly like Java's static and cannot be invoked through an instance.

If you force the compilation to pass, it will look like this.

funmain(args:Array<String>){
       valobj —Super=Sub()
       println (Super.x)
       println(obj.y)
       Sub.methodA()
       obj.methodB()
}

As mentioned above, Super or Sub cannot be called without specifying Sub, so there is no such thing as Super if you thought the properties and methods on the Sub side were referenced like Java.

Although Kotlin is considered to interoperate with Java, it is not "just different grammar but exactly the same as Java."We cannot completely reproduce the behavior of the code, like the Java code in your question, which sometimes produces seemingly strange results that are different from what we expected.


2022-09-30 18:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.