我正在尝试在recyclerView适配器中创建其他构造函数,但是遇到错误,请告诉我如何在回收器适配器中创建二级构造函数,并在kotlin android studio中使用arraylist参数。
class CustomRecyclerAdapter constructor(
val context1: Context,
val topics: Array<String>,
private var alist: ArrayList<Array<String>>
) : RecyclerView.Adapter<Custom
我正在编写一个包含两个不可变值的类,这两个值是在主构造函数中设置的。我想添加一个二级构造函数,它接受一个字符串并对其进行解析,以获得这两个值。但是,我想不出在Kotlin中实现这一点的方法,因为辅助构造函数在解析字符串之前立即调用主构造函数。 在java中,我会在其他构造函数中调用this(a,b),但是Java没有主构造函数。如何添加此功能? class Object (a: double, b:double)
{
val a = a
val b = b
constructor(str: String) //Parsing constructor
{
//Do pa
正在尝试将kotlin测试代码复制到Java。考虑以下类和现有kotlin代码的测试类。
open class ClassX(
val a: ObjectA,
val b: ObjectB,
val c: ObjectC
) {
fun someMethod() {
.....
}
}
以下是其中一个测试
class ClassXTest : ClassX(
a = mock(),
b = mock(),
c = mock()
)
尝试在java中模拟相同的代码。
public class
当使用来自二级子类的超类构造函数时,它是将参数传递给祖父母构造函数还是直接父构造函数?
//top class
public First(type first){
varFirst = first;
}
//child of First
public Second(type second){
super(second); //calls First(second)
}
//child of Second
public Third(type third){
super(third); //calls First(third) or Second(third)?
}
我一直试图为我在kotlin的heelo world程序添加一个二级构造函数。下面是我的代码。(我在网上使用了kotlin ide )
class Test(var name:String){
constructor(age:Int,data:Int){
println("$age $data")
}
fun display(){
println("hello world $name")
}
}
fun main(args: Array&l
我试图理解继承,并有一些问题需要理解。
public class SiteTemplate extends SiteTemplateMethods
public SiteTemplate(String country, String language, HttpServletRequest request){
super();
}
public class SiteTemplateMethods extends Resources
public class Resources extends PropertyFiles
1) PropertyFi
open class Super {
open var name : String = "Name1"
init {
println("INIT block fired with : $name")
name = name.toUpperCase()
println(name)
}
}
class SubClass(newName : String) : Super() {
override var name : String = "Mr. $newName"
我在学习Kotlin的过程中遇到了一个我无法解决的问题。我想在Kotlin中扩展Java类RuntimeException,并且能够在不同的环境中使用它的三个构造函数中的任何一个(基于我想抛出异常时的信息)。在java中,我的类应该是这样的:
public class PhotoLibException extends RuntimeException {
public PhotoLibException(String message, RuntimeException ex) {
super(message, ex);
}
public Photo
我们有两个项目,kotlin项目发布了一个由java导入的包。
在kotlin中,是一个值类,如
@JvmInline
value class CountryId(private val id: UUID) {
override fun toString(): String = id.toString()
companion object { fun empty(): CountryId = CountryId(EMPTY_UUID) }
}
在java中,我们看不到构造函数,或者实例化这个类。我还尝试在Kotlin创建一个工厂来创建它们
class IdentifierFac
在Kotlin中,它在构造函数中调用抽象函数时发出警告,并引用了以下有问题的代码:
abstract class Base {
var code = calculate()
abstract fun calculate(): Int
}
class Derived(private val x: Int) : Base() {
override fun calculate(): Int = x
}
fun main(args: Array<String>) {
val i = Derived(42).code // Expected: 42, ac
在Java中,您可以声明一个私有的最终成员变量,然后从构造函数中初始化它,这是非常有用的,也是非常常见的事情:
class MyClass {
private final int widgetCount;
public MyClass(int widgetCount) {
this.widgetCount = widgetCount;
}
在Kotlin中,如何使用传递给构造函数的值初始化最终成员变量(val类型)?
我试图以一种惯用的方式复制kotlin中的一些java代码。特别是,我希望在重写子setter以供将来使用时使用父setter。在java中,我可以做到:
public class Parent() {
public View rootView;
void setRootView(View view) {
this.rootView = view
}
}
public class Child() extends Parent() {
private FrameLayout layout;
public Child(View r
这是Kotlin中的继承示例。 open class Person() {
var _name : String = ""
var _age : Int = 0
constructor(name: String, age : Int) : this(){
_name = name
_age = age
}
}
class Student() : Person() {
var _university : String = ""
constructor (name : Stri
我找不到更好的标题来描述如何避免这个Kotlin类中的代码重复(需要表达式):
class Person(email: String) {
var email: String = email
set(value) {
require(value.trim().isNotEmpty(), { "The email cannot be blank" })
field = value
}
init {
require(email.trim().isNotEmpty(),
在Kotlin中,我创建泛型类来处理数据获取网络状态,如下所示:
sealed class Resource<T>(
val data: T? = null,
val error: String? = null
) {
class Success<T>(data: T?) : Resource<T>(data)
class Loading<T> : Resource<T>()
class Error<T>(error: String) : Resource<T>(erro
如果我使用一个主构造函数来初始化一个属性,它的setter将不会被调用:
open class Foo(open var bar: Any)
open class Baz(bar: Any) : Foo(bar) {
override var bar: Any
get() = super.bar
set(_) = throw UnsupportedOperationException()
}
...
Baz(1) // works, no setter is called
这也行得通:
open class Foo(bar: Any) {
op
我正在学习Kotlin,我有点被构造器卡住了。我已经写了一个程序来帮助我理解这个概念。程序如下:
open class Animal(){
var name :String = "noname";
constructor(name:String):this(){
this.name = name
}
open fun eat(){
println("$name the animal is eating")
}
}
class Dog: Animal{
constructor():su
我和Class.forName("classname").kotlin一起上了课。
我知道这个类实现了一个Interface,它非常简单(如果您有一个与我想的不同的解决方案,它也会工作得很好)
interface Runner {
fun run(): String
}
当我尝试转换我的类时,使用这个函数,如下所示
val result = (Class.forName("classname").kotlin as Runner).run()
我得到以下错误:
kotlin.reflect.jvm.internal.KClassImpl cannot b
我不明白如何在Java上开发类似如下的Scala代码:
public abstract class A {
protected A() { ... }
protected A(int a) { ... }
}
public abstract class B {
protected B() { super(); }
protected B(int a) { super(a); }
}
public class C extends B {
public C() { super(3); }
}
虽然很清楚如何开发C类,但我不知道如何接收B.帮助。
另外,我正在尝试创
在Kotlin中,我有如下代码:
open class A {
protected open fun B.doSomething() {}
}
class B {}
class C : A() {
override fun B.doSomething() {
print("first, do some C-specific logic")
print("now try to call super implementation. How?")
super.doSomething() // doe
我的设置有问题,但我不知道是什么。从,我有以下信息。
no编译器插件为具有特定注释的类生成一个额外的零参数构造函数。
生成的构造函数是合成的,因此不能直接从Java或Kotlin调用,但可以使用反射调用它。
也就是说,我认为我可以通过java和kotlin反射访问noarg构造函数,但我只能通过java反射访问它。这是我想要的行为,还是我做错了什么?
build.gradle
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.50'
id "org.jetbrains.kot
数组添加应该会从并行计算中受益良多,因为每个元素都可以同时进行计算。然而,假设使用所有CPU和GPU核心的RenderScript运行速度比只使用一个CPU核心的Kotlin慢(因为我没有创建多个线程)。这是所有数组大小的情况。
D: ======== 100 by 100 matrix add (10 times) ========
D: Average time RS: 0.0022388910000000002
D: Average time Kotlin: 2.3658340000000004E-4
D: First run RS: 0.004244063
D: First run K
我正在使用kotlin,并且最近用编译器application /targetSdkVersion 27更新了我的应用程序。在这里,我得到了一些编译错误,例如,上下文现在是可空的(上下文?)而不是非空的。我有一个基片段类,其他片段是从它继承的。拥有这个功能是个坏主意吗?
override fun getContext(): Context {
return super.getContext()!!
}
在父函数中,我看到您有以下代码:
/**
* Return the {@link Context} this fragment is currently associated with