面向对象代码_面向对象的代码生成方法
生活随笔
收集整理的這篇文章主要介紹了
面向对象代码_面向对象的代码生成方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
面向對象代碼
代碼生成是減少無聊任務的不健康負擔的一種常用方法,這些任務常常使我們急切地對代碼苦惱。 我見過的許多代碼生成框架都使用模板替換重復方法,在該模板中,您編寫了模板,以了解生成的代碼文件的外觀,然后替換某些關鍵字并重復其他部分以生成所需的特定文件。
這種讓我煩惱的方法的問題是,要編譯生成的代碼真的很難知道生成的代碼是否可以工作。 您可能已經更改了一個類的名稱,并且突然生成的代碼無法構建。 為了解決此問題, 我啟動了一個名為CodeGen的項目 , 該項目旨在完全面向對象,以便您可以從模板到可執行代碼的所有過程中受益于類型安全。 生成器的主要用例是Speedment軟件 ,但它可以用于各種項目。
考慮以下代碼:
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)) ;應用程序的模型樹是使用bean構建的。 可以將新方法和成員變量添加到樹中,以創建同一類的變體。
當要呈現代碼時,可以輕松地將其傳遞給生成器類。
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();} }每個組件都是作為接口類對實現的,因此您可以動態更改實現,而無需重寫系統的其他部分。
希望這對其他人有幫助!
翻譯自: https://www.javacodegeeks.com/2016/02/object-oriented-approach-code-generation.html
面向對象代碼
總結
以上是生活随笔為你收集整理的面向对象代码_面向对象的代码生成方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 教资小学备案要多久(教资小学备案)
- 下一篇: 安卓手机账户锁怎么解开(安卓手机账户)