加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JavaBean的另一种写法及其ruby版代码生成器

发布时间:2020-12-17 04:11:31 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 ruby代码如下: # 一个类包含:类名,属性列表和内部类指针 class Clazz def initialize(cls_name) @cls_name = cls_name @attrs=[] @lead_cls_names=

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

ruby代码如下: 

# 一个类包含:类名,属性列表和内部类指针 
class Clazz 
def initialize(cls_name) 
@cls_name = cls_name 
@attrs=[] 
@lead_cls_names=nil  #前面有几个前导类名 
end 
def cls_name 
@cls_name 
end 

attr_accessor :attrs,:lead_cls_names 
end 


#属性 
class Attr 
def initialize(type=nil,name=nil) 
@type,@name=type,name 
end 
attr_accessor :type,:name 
end 


class Env 
def initialize() 
@clz_list = []        #类的容器 
@cur_clz = nil        #当前类 
@attr = nil      #当前属性 
@attr_index=0         #当前类的第几个属性 
@clz_index=0      #当前属于第几个类 
@cls_print_queue=nil  #类打印队列 
end 

attr_accessor :clz_list,:cur_clz,:attr,:attr_index,:clz_index 
end 


$env = Env.new 


def print_clz_list              #测试容器数据 
$env.clz_list.each do |clzz| 
lead_cls_names = clzz.lead_cls_names 
print "============" 
lead_cls_names.each do |lead_names| 
print lead_names + "-" 
end 
print "#{clzz.cls_name}============n" 

attrs = clzz.attrs 
attrs.each do |attr| 
puts attr.type + "=" + attr.name 
end 
end 
end 


def print_cls_head(i) 
clazz = $env.clz_list[i] 
lead_clz_names = clazz.lead_cls_names 
lead_num = lead_clz_names.size 
cls_name = clazz.cls_name 
template = "" 
if(i == 0) 
template += "public class #{cls_name}{n" 
else 
template += "n" 
template += "t" * lead_num 
template += "public static class #{cls_name}{n" 
end 

template 

end 


#返回前导类的个数 
def get_lead_num(i) 
lead_cls_names = $env.clz_list[i].lead_cls_names 
lead_num = 0 
if(lead_cls_names == nil) 
lead_num = 0 
else 
lead_num = lead_cls_names.size 
end 

lead_num 
end 


def print_cls_end(i) 
lead_num = get_lead_num(i) 
template = "t" * lead_num + "}nn" 
template 
end 


def print_attrs(i) 
lead_type = "t" * get_lead_num(i) + "t" 
attrs = $env.clz_list[i].attrs 

template = "n" 
attrs.each do |attr| 
type = attr.type 
name = attr.name 
template += "#{lead_type}private #{type} #{name};n"  
end 

template 

end 


def get_template(i) 
clazz = $env.clz_list[i] 
lead_type = "t" * get_lead_num(i) + "t" 
template = "n" 
clazz.attrs.each do |attr| 
first_cap_name = attr.name[0].upcase + attr.name[1..attr.name.size-1] 
template += "#{lead_type}public #{attr.type} get#{first_cap_name}(){n" 
template += "#{lead_type}treturn #{attr.name};n" 
template += "#{lead_type}}nn" 
end 
template 
end 


def builder_template(i=0) 
clazz = $env.clz_list[i] 
cls_name = clazz.cls_name 
instance_name = cls_name[0].downcase + cls_name[1..cls_name.size-1] 
attrs = clazz.attrs 

lead_type = "t" * get_lead_num(i) + "t" 

template = "#{lead_type}public static class Builder{n" 

template += "#{lead_type}tprivate #{cls_name} #{instance_name} = new #{cls_name}();nn" 
attrs.each do |attr| 
first_cap_name = attr.name[0].upcase + attr.name[1..attr.name.size-1] #首字母大写 
template += "#{lead_type}tpublic Builder set#{first_cap_name}(#{attr.type} #{attr.name}){n" 
template += "#{lead_type}tt#{instance_name}.#{attr.name} = #{attr.name};n" 
template += "#{lead_type}ttreturn this;n" 
template += "#{lead_type}t}nn" 
end 

template += "#{lead_type}tpublic #{cls_name} get#{cls_name}(){n" 
template += "#{lead_type}ttreturn #{instance_name};n" 
template +="#{lead_type}t}n" 

template += "#{lead_type}}n" 


template 

end 


#逆序排序 
def get_print_cls_done_queue 
cls_list = $env.clz_list 
loc_cls_list=cls_list.sort{|i,j| j.lead_cls_names.size<=>i.lead_cls_names.size} 
loc_cls_list 
end 


def print_cur_cls_end(cls) 
lead_types="t"*cls.lead_cls_names.size 
#puts "#{cls.lead_cls_names.size}个tabs" 
lead_types+"}n" 
end 




def genBean 
clazz_list=$env.clz_list 
cls_done_queue=get_print_cls_done_queue 
print_queue_size=cls_done_queue.size 
print_queue_index=0 
0.upto(clazz_list.size-1) do |i| 
puts print_cls_head(i) 
puts print_attrs(i) 
puts get_template(i) 
puts builder_template(i) 

#查看是否可以为当前类打印结束标签 
cur_lead_types_num = clazz_list[i].lead_cls_names.size 
print_queue_lead_types_num=cls_done_queue[print_queue_index].lead_cls_names.size 
if(cur_lead_types_num == print_queue_lead_types_num) 
puts print_cur_cls_end(cls_done_queue[print_queue_index]) 
print_queue_index += 1 
end 
end 

#将剩余的未结束类结束其花括号标签 
print_queue_index.upto(print_queue_size-1) do |i| 
puts print_cur_cls_end(cls_done_queue[i]) 
end 

=begin 
(clazz_list.size-1).downto(0) do |i| 
puts print_cls_end(i) 
end 
=end 
end 




=begin 
$clz_list = []     #类的容器 
$cur_clz = nil     #当前类 
$attr = nil  #当前属性 
$attr_index=0      #当前类的第几个属性 
$clz_index=0  #当前属于第几个类 
=end 






File.open("Test.txt"){|f| 
f.each_line do |line| 
line = line.gsub("n","") #去除换行符 


if /^Cls/ =~ line  #碰到以Cls开头,表名以下的属性属于该类 
type_cls_names = line.split(":") #将该行分成两部分,第一部分为Cls,第二部分为类名集合,每个类名之间用-相连 
cls_names = type_cls_names[1].split("-")  #获取类名集合 


cls_name = cls_names[cls_names.size-1]  
$env.cur_clz = Clazz.new(cls_name) 

$env.cur_clz.lead_cls_names = cls_names[0...cls_names.size-1]  #获取前导类名集合 

$env.clz_list[$env.clz_index] = $env.cur_clz;  
$env.clz_index += 1 

$env.attr_index = 0            #重置attr_index 

else               #将该类的属性,填充在该类中 
if $env.cur_clz == nil 
puts "nil" 
else 
attr_type_name = line.split(":") 
attr_type=attr_type_name[0] 
attr_name=attr_type_name[1] 
$env.attr = Attr.new(attr_type,attr_name) 
$env.cur_clz.attrs[$env.attr_index] = $env.attr 
$env.attr_index += 1 


end 
end 

end 

=begin 
print_clz_list 

puts get_template(0) 

puts builder_template 
=end 
genBean 
}


注意,配置文件的要领是:假如是一个类名的话,需要在前面加关键字Cls,如果是某个类的内部类,需要依次在最外部的类用"-"连起来,如: Person-Des-Lib

Cls:Person
String:name
int:age
int:sex
String:description
Cls:Person-Des
int:salary
List<String>:girls
Cls:Person-Des-Lib
int:type
String:shit

最后声称的Java代码如下:

public class Person{


private String name;
private int age;
private int sex;
private String description;


public String getName(){
return name;
}


public int getAge(){
return age;
}


public int getSex(){
return sex;
}


public String getDescription(){
return description;
}


public static class Builder{
private Person person = new Person();


public Builder setName(String name){
person.name = name;
return this;
}


public Builder setAge(int age){
person.age = age;
return this;
}


public Builder setSex(int sex){
person.sex = sex;
return this;
}


public Builder setDescription(String description){
person.description = description;
return this;
}


public Person getPerson(){
return person;
}
}


public static class Des{


private int salary;
private List<String> girls;


public int getSalary(){
return salary;
}


public List<String> getGirls(){
return girls;
}


public static class Builder{
private Des des = new Des();


public Builder setSalary(int salary){
des.salary = salary;
return this;
}


public Builder setGirls(List<String> girls){
des.girls = girls;
return this;
}


public Des getDes(){
return des;
}
}


public static class Lib{


private int type;
private String shit;


public int getType(){
return type;
}


public String getShit(){
return shit;
}


public static class Builder{
private Lib lib = new Lib();


public Builder setType(int type){
lib.type = type;
return this;
}


public Builder setShit(String shit){
lib.shit = shit;
return this;
}


public Lib getLib(){
return lib;
}
}
}


}


}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读