我正在使用Webstorm,我写了一个React组件,我的代码如下:
async onDrop( banner, e ) {
banner.classList.remove( 'dragover' );
e.preventDefault();
const file = e.dataTransfer.files[ 0 ], reader = new FileReader();
const { dispatch } = this.props;
const result = await this.readFile( file, reader );
banner.style.background = `url( ${ result } ) no-repeat center`;
dispatch( addLayer( file ) );
return false;
}
@isImage( 0 )
readFile( file, reader ) {
reader.readAsDataURL( file );
return new Promise( function ( resolve, reject ) {
reader.onload = ( event ) => …Run Code Online (Sandbox Code Playgroud) 我从一个类调用一个方法,它给我一个错误,使方法静态.我很困惑为什么,因为我问这个问题类变量和构造函数中的参数有什么区别?我的理解是类变量是静态的.
患者类:
public String setOption(String option) throws IOException
{
option = stdin.readLine();
//stuff here
return option;
}
Run Code Online (Sandbox Code Playgroud)
患者管理系统:
public class PatientManagementSystem
{
static BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
public static void main(String[] args) throws IOException
{
Patient.setOption(null);
}
}
Run Code Online (Sandbox Code Playgroud)
错误:

我是将方法更改为静态还是创建局部变量?
我需要有从数据库中获取内容的方法,但我不理解PHP中静态函数和普通函数之间的区别.
示例代码
class Item {
public static function getDetail($arg) {
$detail = $this->findProductId($arg);
return $detail;
}
private function findProductId($id) {
//find product_id in database where id = $arg
//return detail of product
}
}
Run Code Online (Sandbox Code Playgroud)
和课外的功能
function getDetail($arg) {
$detail = findProductId($arg);
return $detail;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用$item = Item::getDetail(15);和$item = getDetail(15);- 他们是一样的.
我正在创建avro RDD以下代码.
def convert2Avro(data : String ,schema : Schema) : AvroKey[GenericRecord] = {
var wrapper = new AvroKey[GenericRecord]()
var record = new GenericData.Record(schema)
record.put("empname","John")
wrapper.datum(record)
return wrapper
}
Run Code Online (Sandbox Code Playgroud)
并创建avro RDD如下.
var avroRDD = fieldsRDD.map(x =>(convert2Avro(x, schema)))
Run Code Online (Sandbox Code Playgroud)
执行时,我在上面的行中得到以下异常
Exception in thread "main" org.apache.spark.SparkException: Task not serializable
at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:166)
at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:158)
at org.apache.spark.SparkContext.clean(SparkContext.scala:1242)
at org.apache.spark.rdd.RDD.map(RDD.scala:270)
at org.apache.spark.deploy.SparkSubmit$.launch(SparkSubmit.scala:331)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:75)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.io.NotSerializableException: org.apache.avro.Schema$RecordSchema
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
Run Code Online (Sandbox Code Playgroud)
任何指针?
好基本上我有三个班:
在我要做的主要课程中:
Apple apple = new Apple(String one, String two);
Run Code Online (Sandbox Code Playgroud)
然后Apple类在全球范围内设置它们:
public Apple()
{
//empty constructor
}
public Apple(String one, String two)
{
this.one = one;
this.two = two;
}
Run Code Online (Sandbox Code Playgroud)
然后在Pie类我做:
Apple apple = new Apple();
Run Code Online (Sandbox Code Playgroud)
然后,如果我尝试从Pie类中访问变量'one'或'two',它们将返回null.有人能帮我吗?
作为一般惯例,静态方法是否应该从具有实例方法的类中分离为另一个类?
你的理由还有一个例子吗?
我想知道将方法声明为静态的含义是什么。我知道当声明为静态时,我可以使用该方法而无需实例化该类。但是什么时候应该将方法声明为静态方法以及将方法声明为静态方法会产生什么影响。
预先感谢您对此主题的任何帮助/说明。
所以我试图让一个基本的读者开始工作,这样我就可以在以后的身份验证过程中处理文件。
我遇到的问题是我的 BufferedReader 行出现错误,导致我的 try 函数抛出非法启动异常并且它不会运行。Eclipse 在 br 声明末尾的分号上显示一个错误,并说我应该放一个 { 但我不明白为什么这是必要的。
BufferedReader br = new BufferedReader(new FileReader("Assign4.txt"));
Run Code Online (Sandbox Code Playgroud)
我试图把它放在那里,但它打破了整个尝试部分。
package main;
import java.io.*;
public class file_interface
{
BufferedWriter wr = new BufferedWriter(new FileWriter("target.txt"));
BufferedReader br = new BufferedReader(new FileReader("Assign4.txt"));
try
{
int count = 1;
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null)
{
sb.append(count++);
sb.append(line);
sb.append("\n");
wr.write(line);
line = br.readLine();
}
}
catch (IOException e)
{
System.err.println("Error: " + e);
}
finally
{
br.close();
wr.close(); …Run Code Online (Sandbox Code Playgroud) 根据Oracle的定义,
有时,您希望拥有所有对象共有的变量.这是通过静态修改器完成的.在声明中具有static修饰符的字段称为静态字段或类变量.它们与类相关联,而不是与任何对象相关联.该类的每个实例共享一个类变量,该变量位于内存中的一个固定位置.
通过这个定义,可以安全地推断出一个静态变量属于该类,并且不应该被类的任何对象访问以进行修改.因为所有对象都共享它.
所以来自同一定义的这一行有点令人困惑:
任何对象都可以改变类变量的值......
所以我尝试了这个代码并打印出45(尽管我收到警告说"通过实例引用访问静态成员"):
public class Main {
static int value = 8;
public static void main(String[] args) {
// write your code here
Main main = new Main();
main.value = 45;
System.out.println(value);
}
}
Run Code Online (Sandbox Code Playgroud)
如果这是一个Student类,并且我有一个静态变量numberOfStudents,为什么要允许该类的一个对象更改此类变量的值?
假设您有以下两个类,第一个是Cuboid类,第二个是描述操作的类。
public class Cuboid {
private double length;
private double width;
private double height;
public Cuboid(double length, double width, double height) {
super();
this.length = length;
this.width = width;
this.height = height;
}
public double getVolume() {
return length * width * height;
}
public double getSurfaceArea() {
return 2 * (length * width + length * height + width * height);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不只使用抽象类:
public class Cuboid {
public static double getVolume(double length, double width, double height) {
return …Run Code Online (Sandbox Code Playgroud) 我是java的新手.有一些新的弹出窗口叫做public final void.这是做什么的?public static void和public final void之间有什么区别?我非常感谢你们!
java ×7
methods ×2
oop ×2
static ×2
apache-spark ×1
avro ×1
class ×1
eclipse ×1
ecmascript-6 ×1
javascript ×1
php ×1
salesforce ×1
scala ×1
try-catch ×1