Java基础-3类和对象声明与创建

  一)、在1和2中有粗略介绍过类和对象的概念,在这里简单回顾一下:

    对象与类:一个实际或者虚拟的物体,这个物体既是我们的对象,这个物体呢又是属于一个分,如动物类,人类

  二)、创建对象:

    在创建对象的时候。我们需要先抽象出类,比如我们需要创建一个对象是生物类是人,那么人具有吃饭、睡觉、说话等等属性,所以定义类实现如下:

    

 1 package test;
 2 
 3 public class Man { // 创建一个人类
 4     // 人类拥有的属性吃饭、睡觉、说话
 5     private String EAT;
 6     private String SLEEP;
 7     private String SPEAK;
 8     
 9     // 对人所具有的属性进行封装
10     public String getEat() {
11         return EAT;
12     }
13     public void setEat(String eAT) {
14         this.EAT = eAT;
15     }
16     public String getSleep() {
17         return SLEEP;
18     }
19     public void setSleep(String sLEEP) {
20         this.SLEEP = sLEEP;
21     }
22     public String getSpeak() {
23         return SPEAK;
24     }
25     public void setSpeak(String sPEAK) {
26         this.SPEAK = sPEAK;
27     }
28     
29     //    实现对象
30     public static void main(String[] args) {
31         // TODO Auto-generated method stub
32      Man man=new Man();
33      man.setEat("吃一碗饭");
34      man.setSleep("睡一小时");
35      man.setSpeak("聊会天");
36      System.out.println(man.getEat());
37      System.out.println(man.getSleep());
38      System.out.println(man.getSpeak());     
39     }
40 
41 }
View Code

    在这里我们使用了new关键字和想要创建对象的类名,如:Man man= new Man();

    等号左边以类名Man作为变量类型定义了一个变量man,来指向等号右边通过new关键字创建的一个Man类的实例对象,变量man就是对象的引用,而通过以上步骤我们实例化了一个对象,需要注意的是在new语句的类名后一定要加上(),Man()被称为构造方法,对于封装了的对象方法需要通过get/set方法对其进行读取/赋值

 

 

 

------概念理由有误区的地方还请不吝指教---------

转载于:https://www.cnblogs.com/yangfawang/p/10302584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java 中的类和对象是面向对象编程的基础。类是一种模板,用于创建对象。对象是类的实例。类的声明包括类名、属性和方法。 举个例子,如果我们要创建一个名为“ComplexNumber”的类,该类用于表示复数,其中包含实部和虚部。 ```java class ComplexNumber { double real; double imaginary; public ComplexNumber(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public ComplexNumber add(ComplexNumber c) { return new ComplexNumber(real + c.real, imaginary + c.imaginary); } } ``` 在这个例子中,我们声明了一个名为“ComplexNumber”的类,其中包含两个属性:实部和虚部。还有一个构造函数和一个名为“add”的方法。 然后在你的程序中,你可以创建复数对象并对它们进行加法运算,如下所示: ```java ComplexNumber c1 = new ComplexNumber(1.0, 2.0); ComplexNumber c2 = new ComplexNumber(3.0, 4.0); ComplexNumber c3 = c1.add(c2); ``` 这里c3的值是(4.0,6.0) ### 回答2: 复数运算是指对虚数和实数进行加减乘除等操作的过程,其中虚数是指包含 i 的数。在Java中,我们可以使用类和对象来实现复数运算。 首先,定义一个名为Complex的类来表示复数。该类包含两个成员变量:实部和虚部,都是double类型。另外,还需要实现一些基本的操作函数,例如加减乘除等运算,以及输出结果等。 public class Complex { private double real; // 实部 private double imag; // 虚部 // 构造函数 public Complex(double r, double i) { real = r; imag = i; } // 加法运算 public Complex add(Complex c) { return new Complex(real+c.real, imag+c.imag); } // 减法运算 public Complex subtract(Complex c) { return new Complex(real-c.real, imag-c.imag); } // 乘法运算 public Complex multiply(Complex c) { return new Complex(real*c.real - imag*c.imag, real*c.imag + imag*c.real); } // 除法运算 public Complex divide(Complex c) { double denominator = c.real*c.real + c.imag*c.imag; return new Complex((real*c.real + imag*c.imag)/denominator, (imag*c.real - real*c.imag)/denominator); } // 输出结果 public String toString() { return "(" + real + "," + imag + ")"; } } 接下来,我们可以在主函数中创建复数对象,并进行加减乘除等操作。例如: public static void main(String[] args) { Complex c1 = new Complex(1.0, 2.0); Complex c2 = new Complex(3.0, 4.0); Complex sum = c1.add(c2); Complex diff = c1.subtract(c2); Complex prod = c1.multiply(c2); Complex quot = c1.divide(c2); System.out.println("Sum: " + sum); System.out.println("Diff: " + diff); System.out.println("Prod: " + prod); System.out.println("Quot: " + quot); } 运行以上代码,输出结果如下: Sum: (4.0,6.0) Diff: (-2.0,-2.0) Prod: (-5.0,10.0) Quot: (0.44,0.08) 以上就是使用Java类和对象来实现复数运算的方法,通过定义一个复数类以及实现基本的加减乘除等函数,我们可以轻松地进行复数运算。 ### 回答3: Java类和对象是面向对象编程的基础,学习和掌握类的声明和对象的创建对于程序员来说至关重要。复数运算也是计算机编程中常见的基本运算,理解复数运算并实现其操作可以提高程序的精度和可靠性。 在Java中,复数可以使用类的方式进行声明和定义。我们可以通过声明一个名为Complex(复数)的类来实现: public class Complex{ private double real; //复数的实部 private double imaginary; //复数的虚部 public Complex(double real, double imaginary){ this.real = real; this.imaginary = imaginary; } //获取实部 public double getReal() {return real;} //获取虚部 public double getImaginary() {return imaginary;} //复数加法 public Complex add(Complex other) { double real = this.real + other.getReal(); double imaginary = this.imaginary + other.getImaginary(); return new Complex(real, imaginary); } //复数减法 public Complex subtract(Complex other) { double real = this.real - other.getReal(); double imaginary = this.imaginary - other.getImaginary(); return new Complex(real, imaginary); } //复数乘法 public Complex multiply(Complex other) { double real = this.real * other.getReal() - this.imaginary * other.getImaginary(); double imaginary = this.real * other.getImaginary() + this.imaginary * other.getReal(); return new Complex(real, imaginary); } //复数除法 public Complex divide(Complex other) { double real = (this.real * other.getReal() + this.imaginary * other.getImaginary()) / (Math.pow(other.getReal(),2)+ Math.pow(other.getImaginary(),2)); double imaginary = (this.imaginary * other.getReal() - this.real * other.getImaginary()) / (Math.pow(other.getReal(),2)+ Math.pow(other.getImaginary(),2)); return new Complex(real, imaginary); } //复数的大小(模) public double modulus(){ return Math.sqrt(Math.pow(this.real,2) + Math.pow(this.imaginary,2)); } //复数的共轭 public Complex conjugate(){ return new Complex(this.real,-this.imaginary); } } 在上述代码中,我们定义了一个复数类Complex,包含一个实部和虚部。我们还为该类定义了加法、减法、乘法、除法、模和共轭操作等方法,方便我们在后续的程序中进行实际操作。 下面是复数类的应用范例: Complex c1 = new Complex(1, 2); Complex c2 = new Complex(3, 4); Complex sum = c1.add(c2); System.out.println("c1 + c2 = " + sum.getReal() + " + " + sum.getImaginary() + "i"); Complex difference = c1.subtract(c2); System.out.println("c1 - c2 = " + difference.getReal() + " + " + difference.getImaginary() + "i"); Complex product = c1.multiply(c2); System.out.println("c1 * c2 = " + product.getReal() + " + " + product.getImaginary() + "i"); Complex quotient = c1.divide(c2); System.out.println("c1 / c2 = " + quotient.getReal() + " + " + quotient.getImaginary() + "i"); System.out.println("|c1| = " + c1.modulus()); System.out.println("Conjugate of c2: " + c2.conjugate().getReal() + " + " + c2.conjugate().getImaginary() + "i"); 在上述代码中,我们使用复数类Complex进行复数的加、减、乘、除、模和共轭等操作,并输出结果。使用面向对象编程的思想,可以极大地提高代码的可维护和可读性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值