当前位置: 首页 > news >正文

星沙网站建设公司国际新闻界期刊

星沙网站建设公司,国际新闻界期刊,广州app网站开发,建网购网站本文章就讲的是很基础的类的使用 重点大概就是类的构造函数以及一些很基础的东西。 实验内容是些老生常谈的东西,Complex类,在当初学C面向对象的时候也是这个样子展开的。 内容如以下: public class Complex {float real;float imag;public…

本文章就讲的是很基础的类的使用
重点大概就是类的构造函数以及一些很基础的东西。

实验内容是些老生常谈的东西,Complex类,在当初学C++面向对象的时候也是这个样子展开的。
内容如以下:

public class Complex {float real;float imag;public Complex(){real = 0;imag = 0;}public Complex(float real, float imag){this.real = real;this.imag = imag;}public Complex add(float real){this.real += real;return this;}public Complex add(Complex complex){this.real += complex.real;this.imag += complex.imag;return this;}public Complex sub(float real){this.real -= real;return this;}public Complex sub(Complex complex){this.real -= complex.real;this.imag -= complex.imag;return this;}public Complex mul(float real){this.real *= real;this.imag *= real;return this;}public Complex mul(Complex complex){float TheReal = this.real;float TheImag = this.imag;this.real = TheReal * complex.real - TheImag*complex.imag;this.imag = TheReal * complex.imag + TheImag*complex.real;return this;}public String toString(){String str="";if(this.imag == 0){str += this.real;}else if(this.imag > 0 ){if(this.real==0){str += this.imag + "i";}else str += this.real + "+" + this.imag + "i";}else{if(this.real==0){str += this.imag + "i";}else str += this.real + "" + this.imag + "i";}return str;}}

这里也不得不说一丁点相关知识–重载 overload
如这里的:

	public Complex(){real = 0;imag = 0;}public Complex(float real, float imag){this.real = real;this.imag = imag;}public Complex add(float real){this.real += real;return this;}public Complex add(Complex complex){this.real += complex.real;this.imag += complex.imag;return this;}

这里的Complex()Complex(float real, float imag)构造函数也就用了一些重载的知识。
所谓重载,就是函数名不变但是函数内部的参数变化,我们调用函数时通过函数里面的参数决定我们要调用哪个函数。
同理的,Complex add(float real),Complex add(Complex complex)也是如此,当add()的括号里面是一个float值的时候调用Complex add(float real),括号里面是一个Complex对象的时候调用Complex add(Complex complex)
我当时认为这不是理所当然的吗?其实不是的,这里是用到了重载(overload)的知识。
总代码:
在这里插入图片描述

包com.Class.Work3里的Complex类:

package com.Class.Work3;public class Complex {float real;float imag;public Complex(){real = 0;imag = 0;}public Complex(float real, float imag){this.real = real;this.imag = imag;}public Complex add(float real){this.real += real;return this;}public Complex add(Complex complex){this.real += complex.real;this.imag += complex.imag;return this;}public Complex sub(float real){this.real -= real;return this;}public Complex sub(Complex complex){this.real -= complex.real;this.imag -= complex.imag;return this;}public Complex mul(float real){this.real *= real;this.imag *= real;return this;}public Complex mul(Complex complex){float TheReal = this.real;float TheImag = this.imag;this.real = TheReal * complex.real - TheImag*complex.imag;this.imag = TheReal * complex.imag + TheImag*complex.real;return this;}public String toString(){String str="";if(this.imag == 0){str += this.real;}else if(this.imag > 0 ){if(this.real==0){str += this.imag + "i";}else str += this.real + "+" + this.imag + "i";}else{if(this.real==0){str += this.imag + "i";}else str += this.real + "" + this.imag + "i";}return str;}}

调用Complex类的TestWork3类:

import com.Class.Work3.Complex;public class TestWork3 {public static void main(String[] args) {Complex c = new Complex(1, 2);
//        c.add(5);
//        Complex w = new Complex(4,1);
//        c.sub(w);
//        Complex w2 = new Complex(1,2);
//        c.mul(w2);
//        c.mul(5);System.out.println(c);}
}

就这样了~

http://www.mmbaike.com/news/84890.html

相关文章:

  • 西安网络科技有限公司有哪些营销型网站seo
  • 最新域名解析网站什么是搜索引擎营销
  • 网站首眉怎么做培训课程网站
  • 公安局 网站备案查询系统青岛网站建设方案优化
  • wampserver做网站seo sem是啥
  • wordpress自定义打不开aso安卓优化
  • 网站怎么做切换图片全国新增确诊病例
  • html5公司手机网站模板百度大数据查询
  • 大连手机网站开发网站注册流程
  • 杭州网站建设哪家权威谷歌商店官网
  • 专做hiphop的网站网络营销推广外包平台
  • 做系统的网站全国疫情最新名单
  • 软件公司名字seo关键字怎么优化
  • 芜湖哪些公司做公司网站温州seo顾问
  • 苏州晶体公司网站个人博客搭建
  • 开网站卖茶要怎么做网络推广网址
  • 网站超链接怎么做百度seo查询收录查询
  • 拖拽式建站平台广州优化疫情防控举措
  • 红色简约的手机社区类网站html5响应式模板下载廊坊seo排名
  • dede静态网站六种常见的网站类型
  • 邓亚萍做的网站推广公司品牌
  • 崇州市城乡建设局网站温州seo网站建设
  • 做公务员题哪个网站比较好地推一手项目平台
  • 建设银行手机网站首页网站排名seo
  • 淘客返利网站建设企业查询平台
  • 福州市有哪些制作网站公司企业门户网站模板
  • 个人做信息分类网站需备案吗常用的搜索引擎
  • 如何创造免费网站网站关键词全国各地的排名情况
  • 厦门哪些做鲜花的网站长沙优化网站哪家公司好
  • 公司网站制作企业太原seo排名优化软件