不是静态类,但 /** * Returns the trigonometric sine of an angle. Special cases: * <ul><li>If the argument is NaN or an infinity, then the * result is NaN. * <li>If the argument is zero, then the result is a zero with the * same sign as the argument.</ul> * * <p>The computed result must be within 1 ulp of the exact result. * Results must be semi-monotonic. * * @param a an angle, in radians. * @return the sine of the argument. */ public static double sin(double a) { return StrictMath.sin(a); // default impl. delegates to StrictMath }sin是静态方法所以可以Math.sin() 酱紫调用
C++我不清楚,但在java中,在一个类里可以有成员变量(也可以叫成员属性),成员方法,和构造方法(也叫构造器)。而成员变量可分为类属性(不以static修饰)和实例属性(用static修饰)两种。其中类属性从这个类的准备阶段起开始存在,直到系统完全销毁这个类,类属性的作用域与这个类的生存范围相同,而实实例属性则从这个类的实例被创建开始起存在,直到系统完全销毁这个实例。实例属性的作用域与对应实例的生存范围相同。在访问权限权限(即看变量用什么修饰符限定访问)允许的情况下。实例属性需要通过类的实例去调用。而类变量可以不创建类的实例,直接用类名调用。同样的。成员方法也可分为实例方法和类方法,在访问权限允许的情况下。实例方法需要通过类的实例去调用。而类方法可以不创建类的实例,直接用类名调用。 如下代码: //定义一个Person类class Person { //定义一个实例属性 public String name = "小明"; //定义一个类属性 public static int eyeNum = 2;
//定义实例方法 public void show1() { System.out.println("名字为:"+name); //下面这句将不会出错实例方法内可调用static 修饰的变量或方法 System.out.println("眼睛个数为"+eyeNum); }
public static void show2() { //下面这句将出错类方法内不可调用static 修饰的变量或方法 //System.out.println("名字为:"+this.name); //下面这句将不会出错,类方法内可调用static 修饰的变量或方法 System.out.println("眼睛个数为"+eyeNum); } }//再定义一个测试类public class Test { public static void main(String[] args) { //下面将会出错,因为实例方法不能用类名调用 //Person.show1();
//下面这句将不会出错。因为类方法可以直接用类名调用,不需要创建实例 Person.show2(); //show1()这个实例方法应该创建Person类的实例后,再用实例去调用 Person p = new Person(); p.show1();
} }说了这些,希望LZ能明白Math类中的sin()方法为什么要直接用类名Math去调用。因为sin()就是一个类方法LZ可以去查下API文档,Math类里的方法都有详细信息static double sin(double a) 返回角的三角正弦。 你可看到sin方法就是用static修饰的(补充几点:上面所说的类方法,有人也叫静态方法。类的实例也可以叫类的对象。这些叫法希望LZ能明白。用的是外国人的东西。翻译过来叫法出现不一很正常) 希望能帮到LZ,有不懂的再问哈
* Returns the trigonometric sine of an angle. Special cases:
* <ul><li>If the argument is NaN or an infinity, then the
* result is NaN.
* <li>If the argument is zero, then the result is a zero with the
* same sign as the argument.</ul>
*
* <p>The computed result must be within 1 ulp of the exact result.
* Results must be semi-monotonic.
*
* @param a an angle, in radians.
* @return the sine of the argument.
*/
public static double sin(double a) {
return StrictMath.sin(a); // default impl. delegates to StrictMath
}sin是静态方法所以可以Math.sin() 酱紫调用
如下代码:
//定义一个Person类class Person { //定义一个实例属性
public String name = "小明"; //定义一个类属性
public static int eyeNum = 2;
//定义实例方法
public void show1() {
System.out.println("名字为:"+name); //下面这句将不会出错实例方法内可调用static 修饰的变量或方法
System.out.println("眼睛个数为"+eyeNum);
}
public static void show2() { //下面这句将出错类方法内不可调用static 修饰的变量或方法
//System.out.println("名字为:"+this.name); //下面这句将不会出错,类方法内可调用static 修饰的变量或方法
System.out.println("眼睛个数为"+eyeNum); }
}//再定义一个测试类public class Test {
public static void main(String[] args) {
//下面将会出错,因为实例方法不能用类名调用
//Person.show1();
//下面这句将不会出错。因为类方法可以直接用类名调用,不需要创建实例
Person.show2();
//show1()这个实例方法应该创建Person类的实例后,再用实例去调用
Person p = new Person();
p.show1();
}
}说了这些,希望LZ能明白Math类中的sin()方法为什么要直接用类名Math去调用。因为sin()就是一个类方法LZ可以去查下API文档,Math类里的方法都有详细信息static double sin(double a)
返回角的三角正弦。 你可看到sin方法就是用static修饰的(补充几点:上面所说的类方法,有人也叫静态方法。类的实例也可以叫类的对象。这些叫法希望LZ能明白。用的是外国人的东西。翻译过来叫法出现不一很正常)
希望能帮到LZ,有不懂的再问哈