日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

面向对象的代码生成方法

發(fā)布時間:2023/12/3 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 面向对象的代码生成方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

代碼生成是減少無聊任務(wù)的不健康負(fù)擔(dān)的一種常用方法,這些任務(wù)常常使我們急切的代碼苦惱。 我見過的許多代碼生成框架都使用模板替換重復(fù)方法,在該方法中,您可以編寫一個模板,以了解生成的代碼文件的外觀,然后替換某些關(guān)鍵字并重復(fù)其他部分以生成所需的特定文件。

這種讓我煩惱的方法的問題是,要編譯生成的代碼真的很難知道生成的代碼是否可以正常工作。 您可能已經(jīng)更改了一個類的名稱,并且突然生成的代碼無法構(gòu)建。 為了解決這個問題, 我啟動了一個名為CodeGen的項(xiàng)目 , 該項(xiàng)目旨在完全面向?qū)ο?#xff0c;以便從模板到可執(zhí)行代碼的所有類型安全中受益。 生成器的主要用例是Speedment軟件 ,但它可以用于各種項(xiàng)目。

考慮以下代碼:

final Generator generator = new JavaGenerator();final File file = File.of("org/example/Foo.java").add(Class.of("Foo").public_().add(Field.of("x", DOUBLE_PRIMITIVE).final_()).add(Field.of("y", DOUBLE_PRIMITIVE).final_()).add(Field.of("z", DOUBLE_PRIMITIVE).final_()).call(new AutoConstructor()).call(new AutoSetGetAdd()).call(new AutoEquals())).call(new AutoJavadoc()).call(new AutoImports(generator)) ;

應(yīng)用程序的模型樹是使用bean構(gòu)建的。 可以將新方法和成員變量添加到樹中,以創(chuàng)建同一類的變體。

當(dāng)要呈現(xiàn)代碼時,可以輕松地將其傳遞給生成器類。

String code = generator.on(file).get();

生成的代碼如下所示:

/*** Write some documentation here.*/ package org.example;import java.util.Optional;/*** @author You name here*/ public class Foo {private final double x;private final double y;private final double z;/*** Initializes the Foo component.** @param x the x* @param y the y* @param z the z*/public Foo(double x, double y, double z) {this.x = x;this.y = y;this.z = z;}/*** Returns the value of x.** @return the value of x*/public double getX() {return x;}/*** Sets a new value for x.** @param x the new value of x*/public void setX(double x) {this.x = x;}/*** Returns the value of y.** @return the value of y*/public double getY() {return y;}/*** Sets a new value for y.** @param y the new value of y*/public void setY(double y) {this.y = y;}/*** Returns the value of z.** @return the value of z*/public double getZ() {return z;}/*** Sets a new value for z.** @param z the new value of z*/public void setZ(double z) {this.z = z;}/*** Generates a hashCode for this object. If any field is* changed to another value, the hashCode may be different.* Two objects with the same values are guaranteed to have* the same hashCode. Two objects with the same hashCode are* not guaranteed to have the same hashCode."** @return the hash code*/@Overridepublic int hashCode() {int hash = 7;hash = 31 * hash + (Double.hashCode(this.x));hash = 31 * hash + (Double.hashCode(this.y));hash = 31 * hash + (Double.hashCode(this.z));return hash;}/*** Compares this object with the specified one for equality.* The other object must be of the same type and not null for* the method to return true.** @param other the object to compare with* @return {@code true} if the objects are equal*/@Overridepublic boolean equals(Object other) {return Optional.ofNullable(other).filter(o -> getClass().equals(o.getClass())).map(o -> (Foo) o).filter(o -> this.x == o.x).filter(o -> this.y == o.y).filter(o -> this.z == o.z).isPresent();} }

每個組件都實(shí)現(xiàn)為一個接口類對,因此您可以動態(tài)更改實(shí)現(xiàn)而無需重寫系統(tǒng)的其他部分。

希望這對其他人有幫助!

翻譯自: https://www.javacodegeeks.com/2016/02/object-oriented-approach-code-generation.html

總結(jié)

以上是生活随笔為你收集整理的面向对象的代码生成方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。