[英]Class Variables and Class Methods in Java, My Fault
Class; 类;
package com.eteration.model;
public class Circle {
private static double PI;
private int radius;
private double area;
public double calculateArea(){
return radius*radius*PI;
}
public static double getPI() {
return PI;
}
public static void setPI(double pI) {
PI = pI;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
}
Scrapbook; 剪贴簿;
Circle.PI=3.14;
System.out.println(Circle.getPI());
Circle circle1=new Circle(7);
System.out.println(circle1.getPI());
circle1.PI=3.1415;
Circle circle2=new Circle(3);
System.out.println(circle1.getPI());
System.out.println(circle2.getPI());
circle2.setPI(3.141592);
System.out.println(circle1.getPI());
System.out.println(circle2.getPI());
System.out.println(Circle.getPI());
I am trying exercise. 我正在尝试运动。 In this exercise, I create a class, after I am coding example to scrapbook.
在本练习中,我将示例编码为剪贴簿后,创建了一个类。 But , I am seeing fault.
但是,我看到了错误。 These error;
这些错误; "Circle cannot be resaloved to a variable, Circle cannot be resolved, Circle cannot be resolved a type."
“圆形无法重新分配给变量,圆形无法解析,圆形无法解析为类型。” Help me.
帮我。
you just can not do this Circle circle1=new Circle(7);
你就是不能做这个
Circle circle1=new Circle(7);
because you don't have defined a constructor for the class circle taking an integer.... 因为您尚未为使用整数的类圈定义构造函数。
I would modify the class improving it a little bit like this: 我将修改类,将其改进如下:
Make the constant PI public, and remove setters and getter, you will be able to access to it just like doing Circle.PI
公开不变的PI,并删除设置方法和获取方法,就可以像使用
Circle.PI
一样访问它
class Circle { public static double PI = 3.1415927; private int radius; private double area; public double calculateArea() { return radius * radius * PI; } public Circle(int radius) { this.radius = radius; } public int getRadius() { return radius; } public void setRadius(int radius) { this.radius = radius; } public double getArea() { return area; } public void setArea(double area) { this.area = area; } }
after that the scrapbook will look like: 之后,剪贴簿将如下所示:
Class; 类;
package com.eteration.model; 包com.eteration.model;
public class Circle { private static double PI; 公共类Circle {私有静态双PI; private int radius;
私有整数半径; private double area;
私人双人间
public double calculateArea(){
return radius*radius*PI;
}
public static double getPI() {
return PI;
}
public static void setPI(double pI) {
PI = pI;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
} Scrapbook; }剪贴簿;
//Circle.PI=3.14; // dont need this
System.out.println(Circle.PI ); // instead of getPI()
Circle circle1 = new Circle(7); //the same
//System.out.println(circle1.getPI()); //dont need it since is a class constant
//circle1.PI=3.1415; // dont need it
Circle circle2=new Circle(3); //ok
System.out.println(circle1.getPI());//dont need it
System.out.println(circle2.getPI());//dont need it
circle2.setPI(3.141592);//dont need it
System.out.println(circle1.getPI());//dont need it
System.out.println(circle2.getPI());//dont need it
System.out.println(Circle.getPI());//dont need it
You are trying to access a static private variab!e outside of a class through an object. 您试图通过对象访问类外部的静态私有变量。 If you want to change PI without your set method, you need to make PI either package-scoped(by writing nothing before it) or make it public.
如果要在没有设置方法的情况下更改PI,则需要使PI成为程序包范围的(通过在其之前未写任何内容)或使其公开。 You cannot call static varibles and methods with the object.
您不能使用该对象调用静态变量和方法。 So the right way to access PI or getPI() is through the class eg.:
因此,访问PI或getPI()的正确方法是通过类,例如:
Circle.PI
Circle.getPI()
I recommend to not change the visibility of your variables and just access them through the set methods. 我建议不要更改变量的可见性,而只是通过set方法访问它们。 I would also recommend to make PI final.
我还建议将PI最终化。 By using Java API's Math.PI:
通过使用Java API的Math.PI:
private static final double PI = Math.PI;
Or just use Math.PI. 或仅使用Math.PI。
I forgot, since you do not have a constructor defined you cannot give it a value like you did there: 我忘记了,由于您没有定义构造函数,因此无法像在此那样为它提供一个值:
Circle circle3 = new Circle(3); // the 3 cannot be put there.
I strongly advise to get yourself an IDE like [eclipse][1] 我强烈建议让自己成为一个[eclipse] [1]之类的IDE。
[1]: http://www.eclipse.org "eclipse", it marks wrong syntax, such as using static methods with objects or calling constructors that do not exist. [1]: http : //www.eclipse.org “ eclipse”,它标记了错误的语法,例如对对象使用静态方法或调用不存在的构造函数。
Beest regards. 问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.