如何在 Java/Groovy 中调用父方法
How to call parent methods in Java/Groovy
我是 Java 和 groovy 的新手,我主要在 Python.I 中编写代码,试图理解为什么代码不起作用。
我得到的错误是
groovy.lang.MissingMethodException: No signature of method: static HelloWorld.TestingProduct() is applicable for argument types: () values: []
我的任务是将事件日期添加为项目的系统日期,我只是想通过本地测试了解如何添加它
import groovy.transform.ToString
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
public class HelloWorld{
public static void main(String[] args){
String testin = TestingProduct().Inventory()
System.out.println(testin);
}
}
class Parent {
private String setDateNow() {
OffsetDateTime now = OffsetDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
return formatter.format(now);
}
}
class TestingProduct extends Parent {
private static ProductInventoryEvent Inventory(){
def invent = new ProductInventoryEvent(
productId:'1',
productIdType:'2',
eventType:'3',
eventDate: setDateNow(),
)
return invent
}
}
@Canonical()
@ToString(includeNames = true)
class ProductInventoryEvent {
String productId
String productIdType
String eventType
String eventDate
}
您的代码中几乎没有错误。让我先从 HelloWorld
class.
开始
您正试图以错误的方式 access/call TestingProduct
的静态方法 Inventory
。为了访问静态方法,您不需要 class 的任何对象实例。因此,您应该使用 TestingProduct.Inventory()
.
而不是 TestingProduct().Inventory()
第二件事是,您的方法 Inventory()
返回 ProductInventoryEvent
而不是字符串。所以你应该改变你想要初始化的变量的类型。代码应如下所示:
public class HelloWorld {
public static void main(String[] args){
ProductInventoryEvent testin = TestingProduct.Inventory();
System.out.println(testin);
}
}
您应该更改的另一件事是 TestingProduct
class 中 Inventory()
方法的访问修饰符。您应该使用 package private 或 public 访问修饰符,而不是 private。所以代码应该是这样的:
public class TestingProduct {
static ProductInventoryEvent Inventory(){
def invent = new ProductInventoryEvent(
productId:'1',
productIdType:'2',
eventType:'3',
eventDate: setDateNow(),
)
return invent;
}
}
另外我认为,你不能调用父类 class 中的私有方法。您必须将访问修饰符更改为 protected
我是 Java 和 groovy 的新手,我主要在 Python.I 中编写代码,试图理解为什么代码不起作用。
我得到的错误是
groovy.lang.MissingMethodException: No signature of method: static HelloWorld.TestingProduct() is applicable for argument types: () values: []
我的任务是将事件日期添加为项目的系统日期,我只是想通过本地测试了解如何添加它
import groovy.transform.ToString
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
public class HelloWorld{
public static void main(String[] args){
String testin = TestingProduct().Inventory()
System.out.println(testin);
}
}
class Parent {
private String setDateNow() {
OffsetDateTime now = OffsetDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
return formatter.format(now);
}
}
class TestingProduct extends Parent {
private static ProductInventoryEvent Inventory(){
def invent = new ProductInventoryEvent(
productId:'1',
productIdType:'2',
eventType:'3',
eventDate: setDateNow(),
)
return invent
}
}
@Canonical()
@ToString(includeNames = true)
class ProductInventoryEvent {
String productId
String productIdType
String eventType
String eventDate
}
您的代码中几乎没有错误。让我先从 HelloWorld
class.
您正试图以错误的方式 access/call TestingProduct
的静态方法 Inventory
。为了访问静态方法,您不需要 class 的任何对象实例。因此,您应该使用 TestingProduct.Inventory()
.
TestingProduct().Inventory()
第二件事是,您的方法 Inventory()
返回 ProductInventoryEvent
而不是字符串。所以你应该改变你想要初始化的变量的类型。代码应如下所示:
public class HelloWorld {
public static void main(String[] args){
ProductInventoryEvent testin = TestingProduct.Inventory();
System.out.println(testin);
}
}
您应该更改的另一件事是 TestingProduct
class 中 Inventory()
方法的访问修饰符。您应该使用 package private 或 public 访问修饰符,而不是 private。所以代码应该是这样的:
public class TestingProduct {
static ProductInventoryEvent Inventory(){
def invent = new ProductInventoryEvent(
productId:'1',
productIdType:'2',
eventType:'3',
eventDate: setDateNow(),
)
return invent;
}
}
另外我认为,你不能调用父类 class 中的私有方法。您必须将访问修饰符更改为 protected