static关键字对象
Posted qdkogmsdlfs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了static关键字对象相关的知识,希望对你有一定的参考价值。
题目1:编写一个类Computer,类中含有一个求n的阶乘的方法。将该类打包,并在另一包中的Java文件App.java中引入包,在主类中定义Computer类的对象,调用求n的阶乘的方法(n值由参数决定),并将结果输出。
/*创建computer类,封装在qqq包中,用for循环计算a的阶乘,用public int getA(int x) 语句获取主类中x
的数值,并计算阶乘结果,返回a*/
package qqq;
的数值,并计算阶乘结果,返回a*/
package qqq;
public class Computer {
int a=1;
public int getA(int x) {
for(int i=1;i<x+1;i++) {
a=a*i;
}
return a;
}
}
int a=1;
public int getA(int x) {
for(int i=1;i<x+1;i++) {
a=a*i;
}
return a;
}
}
/*接收computer类, Computer b=new Computer();声明对象, Scanner input=new Scanner(System.in);
int n=input.nextInt();用户输入数值为n赋值, System.out.println("结果为:"+b.getA(n));输出b的值*/
package qqq;
import java.util.*;
import qqq.Computer;
public class aaa {
public static void main(String args[]){
System.out.println("输入一个数字");
Computer b=new Computer();
Scanner input=new Scanner(System.in);
int n=input.nextInt();
System.out.println("结果为:"+b.getA(n));
}
}
int n=input.nextInt();用户输入数值为n赋值, System.out.println("结果为:"+b.getA(n));输出b的值*/
package qqq;
import java.util.*;
import qqq.Computer;
public class aaa {
public static void main(String args[]){
System.out.println("输入一个数字");
Computer b=new Computer();
Scanner input=new Scanner(System.in);
int n=input.nextInt();
System.out.println("结果为:"+b.getA(n));
}
}
运行结果
题目2:
设计一个MyPoint类,表示一个具有x坐标和y坐标的点,该类包括:
- 两个私有成员变量x和y表示坐标值;
- 成员变量x和y的访问器和修改器
- 无参构造方法创建点(0,0);
- 一个有参构造方法,根据参数指定坐标创建一个点;
- distance方法(static修饰)返回参数为MyPoint类型的两个点对象之间的距离。
编写主类Test,在主类中输入两点坐标,创建两个点对象,利用distance()方法计算这两个点之间的距离。
/*从主类zzz中获取数,在qqq中利用public static double distance(ccc A, ccc B) {
double distance = Math.sqrt((A.x - B.x) * (A.x - B.x) + (A.y + B.y) * (A.y + B.y));
return distance;方法计算两点间的距离,然后返回主类zzz输出*/
package qqq;
import java.util.Scanner;
public class ccc {
double x;
double y;
public ccc() {
x = 0.0;
y = 0.0;
}
double x;
double y;
public ccc() {
x = 0.0;
y = 0.0;
}
public ccc(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
return y;
}
public void setY(double y) {
this.y = y;
}
public static double distance(ccc A, ccc B) {
double distance = Math.sqrt((A.x - B.x) * (A.x - B.x) + (A.y + B.y) * (A.y + B.y));
return distance;
}
this.y = y;
}
public static double distance(ccc A, ccc B) {
double distance = Math.sqrt((A.x - B.x) * (A.x - B.x) + (A.y + B.y) * (A.y + B.y));
return distance;
}
}
测试类
package qqq;
import qqq.MyPoint;
import java.util.Scanner;
import java.util.Scanner;
public class zzz {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ccc[] p = new ccc[2];
System.out.println("输入第一个坐标");
double x = input.nextDouble();
double y = input.nextDouble();
p[0] = new ccc(x, y);
System.out.println("输入第二个坐标");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
p[1] = new ccc(x, y);
System.out.println("点(" + p[0].x + "," + p[0].y + ")" + "与点" + "(" + p[1].x + "," + p[1].y + ")" + "的距离是 "
+ ccc.distance(p[0], p[1]));
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ccc[] p = new ccc[2];
System.out.println("输入第一个坐标");
double x = input.nextDouble();
double y = input.nextDouble();
p[0] = new ccc(x, y);
System.out.println("输入第二个坐标");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
p[1] = new ccc(x, y);
System.out.println("点(" + p[0].x + "," + p[0].y + ")" + "与点" + "(" + p[1].x + "," + p[1].y + ")" + "的距离是 "
+ ccc.distance(p[0], p[1]));
}
}
运行截图
以上是关于static关键字对象的主要内容,如果未能解决你的问题,请参考以下文章