1. public class Complex {
    2. private double x,y;
    3. public Complex(double real,double imaginary){
    4. x=real;
    5. y=imaginary;
    6. }
    7. public String toString(){
    8. return "("+x+","+y+"i)";
    9. }
    10. public Complex add(Complex a){
    11. return new Complex(x+a.x,y+a.y);
    12. }
    13. public Complex add(double x,double y){
    14. return new Complex(this.x+x,this.y+y);
    15. }
    16. public static Complex add(Complex a1,Complex a2){
    17. return new Complex(a1.x+a2.x,a1.y+a2.y);
    18. }
    19. public Complex multiply(Complex a){
    20. double X1=this.x*a.x-this.y*a.y;
    21. Double Y1=this.x*a.y+this.y*a.x;
    22. return new Complex(X1,Y1);
    23. }
    24. public Complex multiply(double x,double y){
    25. double X1=this.x*x-this.y*y;
    26. Double Y1=this.x*y+this.y*x;
    27. return new Complex(X1,Y1);
    28. }
    29. public static Complex multiply(Complex a,Complex b){
    30. double X1=a.x*b.x-a.y*b.y;
    31. Double Y1=a.x*b.y+a.y*b.x;
    32. return new Complex(X1,Y1);
    33. }
    34. public double ABC(){
    35. return Math.sqrt(this.x*this.x+this.y+this.y);
    36. }
    37. public static void main(String[] args) {
    38. Complex x,y,z;
    39. x=new Complex(4,5);
    40. y=new Complex(3.4,2.8);
    41. System.out.println("加法:");
    42. z=add(x,y);
    43. System.out.println("resulu1="+z);
    44. z=x.add(y);
    45. System.out.println("resulu2="+z);
    46. z=x.add(6,8);
    47. System.out.println("resulu3="+z);
    48. System.out.println("乘法:");
    49. Complex y1=new Complex(1,2);
    50. System.out.println("multiply(Complex a):"+x.multiply(y1));
    51. System.out.println("multiply(double x,double y):"+x.multiply(1.0,2.0));
    52. System.out.println("static Complex multiply(Complex a,Complex b):"+multiply(x,y1));
    53. System.out.printf("x的模长为:%.4f",x.ABC());
    54. }
    55. }
    1. public class Person {
    2. String name;
    3. String sex;
    4. int age;
    5. public Person(String name,String sex,int age){
    6. this.name=name;
    7. this.sex=sex;
    8. this.age=age;
    9. }
    10. public String toString(){
    11. return "姓名:"+name+" 性别:"+sex+" 年龄:"+age;
    12. }
    13. public static void main(String[] args) {
    14. Person P=new Person("张三","男",18);
    15. System.out.println(P);
    16. Teacher PT=new Teacher("张三","男",18,"教授","计算机");
    17. System.out.println(PT);
    18. Student PS=new Student("张三","男",18,4845148,"2020/9/30","计算机");
    19. System.out.println(PS);
    20. }
    21. }
    22. class Teacher extends Person{
    23. String Zhicheng;
    24. String department;
    25. public Teacher(String name,String sex,int age, String Zhicheng,String department){
    26. super(name,sex,age);
    27. this.Zhicheng=Zhicheng;
    28. this.department=department;
    29. }
    30. public String toString(){
    31. return "姓名:"+name+" 性别:"+sex+" 年龄:"+age+" 职称:"+Zhicheng+" 部门:"+department;
    32. }
    33. }
    34. class Student extends Person{
    35. int Num;
    36. String ctime;
    37. String specialized;
    38. public Student(String name,String sex,int age,int Num,String ctime, String specialized){
    39. super(name,sex,age);
    40. this.Num=Num;
    41. this.ctime=ctime;
    42. this.specialized=specialized;
    43. }
    44. public String toString(){
    45. return "姓名:"+name+" 性别:"+sex+" 年龄:"+age+" 学号:"+Num+" 入学时间:"+ctime+" 专业:"+specialized;
    46. }
    47. }
    1. public class Point {
    2. public int x;
    3. public int y;
    4. public Point(int a,int b){
    5. this.x=a;
    6. this.y=b;
    7. } //构造函数
    8. public double distance(Point P){
    9. return Math.sqrt(((this.x-P.x)*(this.x-P.x)+(this.y-P.y)*(this.y-P.y)));
    10. }
    11. public double distance(int X1,int Y1){
    12. double d=(this.x-X1)*(this.x-X1)+(this.y-Y1)*(this.y-Y1);
    13. //System.out.println(d);
    14. d=Math.sqrt(d);
    15. return d;
    16. }
    17. public static double distance(Point X,Point Y){
    18. double d=(double)(X.x-Y.x)*(X.x-Y.x)+(X.y-Y.y)*(X.y-Y.y);
    19. d=Math.sqrt(d);
    20. //System.out.println(X.x);
    21. return d;
    22. }
    23. public static void main(String[] args) {
    24. Point P1=new Point(4,5);
    25. Point P2=new Point(9,10);
    26. System.out.printf("distance(Point P):d=%.4f\n",P1.distance(P2));
    27. System.out.printf("distance(int x,int y):d=%.4f\n",P1.distance(9,10));
    28. System.out.printf("distance(Point X,Point Y):d=%.4f\n",distance(P1,P2));
    29. }
    30. }